Kwik Core Java
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
- class Simple1 extends Thread{
- public void run(){
- System.out.println("task one");
- }
- }
- class Simple2 extends Thread{
- public void run(){
- System.out.println("task two");
- }
- }
- class TestMultitasking3{
- public static void main(String args[]){
- Simple1 t1=new Simple1();
- Simple2 t2=new Simple2();
- t1.start();
- t2.start();
- }
- }
Output:task one task two
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
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
Note: Each thread run in a separate callstack.
What if we call run() method directly instead start() method?
|
- class TestCallRun1 extends Thread{
- public void run(){
- System.out.println("running...");
- }
- public static void main(String args[]){
- TestCallRun1 t1=new TestCallRun1();
- t1.run();//fine, but does not start a separate call stack
- }
- }
Output:running...
Problem if you direct call run() method
- class TestCallRun2 extends Thread{
- public void run(){
- for(int i=1;i<5;i++){
- try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
- System.out.println(i);
- }
- }
- public static void main(String args[]){
- TestCallRun2 t1=new TestCallRun2();
- TestCallRun2 t2=new TestCallRun2();
- t1.run();
- t2.run();
- }
- }
Output:1 2 3 4 5 1 2 3 4 5
Can we start a thread twice ?
No. After starting a thread, it can never be started again. If you does so, an IllegalThreadStateException is thrown. In such case, thread will run once but for second time, it will throw exception.
Let's understand it by the example given below:
- public class TestThreadTwice1 extends Thread{
- public void run(){
- System.out.println("running...");
- }
- public static void main(String args[]){
- TestThreadTwice1 t1=new TestThreadTwice1();
- t1.start();
- t1.start();
- }
- }
- Output:
running Exception in thread "main" java.lang.IllegalThreadStateException
Subscribe to:
Posts (Atom)