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
Program of performing single task by multiple threads
- class TestMultitasking1 extends Thread{
- public void run(){
- System.out.println("task one");
- }
- public static void main(String args[]){
- TestMultitasking1 t1=new TestMultitasking1();
- TestMultitasking1 t2=new TestMultitasking1();
- TestMultitasking1 t3=new TestMultitasking1();
- t1.start();
- t2.start();
- t3.start();
- }
- }
Output:task one task one task one
- class TestMultitasking2 implements Runnable{
- public void run(){
- System.out.println("task one");
- }
- public static void main(String args[]){
- Thread t1 =new Thread(new TestMultitasking2());//passing annonymous object of TestMultitasking2 class
- Thread t2 =new Thread(new TestMultitasking2());
- t1.start();
- t2.start();
- }
- }
Output:task one task one
No comments:
Post a Comment