Javatpoint Logo
Javatpoint Logo

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
Output:task one
       task one

Note: Each thread run in a separate callstack.

MultipleThreadsStack

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
Output:task one
       task two

Same example as above by annonymous class that extends Thread class:

Program of performing two tasks by two threads
Test it Now
Output:task one
       task two

Same example as above by annonymous class that implements Runnable interface:

Program of performing two tasks by two threads
Test it Now
Output:task one
       task two
Next TopicGarbage Collection




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials