How to perform single task by multiple threads?
If you have to perform single task by many threads, have only one run() method.For example: |
Program of performing single task by multiple threads
Test it Now
Output:task one
task one
task one
Program of performing single task by multiple threads
Test it Now
Note: Each thread run in a separate callstack.
How to perform multiple tasks by multiple threads (multitasking in multithreading)?
If you have to perform multiple tasks by multiple threads,have multiple run() methods.For example: |
Program of performing two tasks by two threads
Test it Now
Same example as above by annonymous class that extends Thread class: Program of performing two tasks by two threads
Test it Now
Same example as above by annonymous class that implements Runnable interface: Program of performing two tasks by two threads
Test it Now
|