How to do things AI Noob vs. Pro
List biggest files Free Open Source: Swiss File Knifea command line
Depeche View
command line
free external tools,
cpp sources
articles |
How to create one or more threads in Java with a few lines of code. Free source code examples with simple instant solutions.
starting point: a simple java program, having just one thread - the main thread.
:load csj01x1.java
output:
thread main step 0 thread main step 1 thread main step 2 thread main step 3 ...solution 01: creating two parallel threads by implementing the Runnable interface, i.e. adding a "run" method. :load csj01x2.java output: thread Thread-0 step 0 thread Thread-1 step 0 thread Thread-0 step 1 thread Thread-1 step 1 thread Thread-0 step 2 thread Thread-1 step 2 thread Thread-0 step 3 thread Thread-1 step 3 ... solution 02: alternatively, we may create two parallel threads by deriving our class from the "Thread" class. in this case, we also have to provide a "run" method, because Thread uses the Runnable interface. :load csj01x3.java this produces the same output as solution 01, but it has one tradeoff: as we derive our class from Thread, we can no longer derive it from anything else. therefore solution 01 is more flexible. |