1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/* Create New Thread Using Runnable Example This Java example shows how to create a new thread by implementing Java Runnable interface. */ /* * To create a thread using Runnable, a class must implement * Java Runnable interface. */ public class CreateThreadRunnableExample implements Runnable{ /* * A class must implement run method to implement Runnable * interface. Signature of the run method is, * * public void run() * * Code written inside run method will constite a new thread. * New thread will end when run method returns. */ public void run(){ for(int i=0; i < 5; i++){ System.out.println("Child Thread : " + i); try{ Thread.sleep(50); } catch(InterruptedException ie){ System.out.println("Child thread interrupted! " + ie); } } System.out.println("Child thread finished!"); } public static void main(String[] args) { /* * To create new thread, use * Thread(Runnable thread, String threadName) * constructor. * */ Thread t = new Thread(new CreateThreadRunnableExample(), "My Thread"); /* * To start a particular thread, use * void start() method of Thread class. * * Please note that, after creation of a thread it will not start * running until we call start method. */ t.start(); for(int i=0; i < 5; i++){ System.out.println("Main thread : " + i); try{ Thread.sleep(100); } catch(InterruptedException ie){ System.out.println("Child thread interrupted! " + ie); } } System.out.println("Main thread finished!"); } } /* Typical output of this thread example would be Main thread : 0 Child Thread : 0 Child Thread : 1 Main thread : 1 Main thread : 2 Child Thread : 2 Child Thread : 3 Main thread : 3 Main thread : 4 Child Thread : 4 Child thread finished! Main thread finished! */ |
Hi,
Please verify the sample output for the “CreateRunnableThreadExample” as it does not seem quite right. I am learning to use threads currently and stumbled upon this link. The example did not seem right to me as per what I have read about threads today. Please do check.
Thanks
it’s output:
Main thread : 0
Child Thread : 0
Child Thread : 1
Main thread : 1
Child Thread : 2
Child Thread : 3
Main thread : 2
Child Thread : 4
Child thread finished!
Main thread : 3
Main thread : 4
Main thread finished!
The output shown is not correct. so, please check it by your own
Well that’s rather depressing.
The output wont be similar for every execution of above code….this because simultaneous execution of both thread once Main thread started..
output;
Main thread : 0
Child Thread : 0
Child Thread : 1
Child Thread : 2
Main thread : 1
Child Thread : 3
Main thread : 2
Child Thread : 4
Child thread finished!
Main thread : 3
Main thread : 4
Main thread finished!
/*
Create New Thread Using Runnable Example
This Java example shows how to create a new thread by implementing
Java Runnable interface.
*/
/*
* To create a thread using Runnable, a class must implement
* Java Runnable interface.
*/
public class CreateThreadRunnableExample implements Runnable{
/*
* A class must implement run method to implement Runnable
* interface. Signature of the run method is,
*
* public void run()
*
* Code written inside run method will constite a new thread.
* New thread will end when run method returns.
*/
public void run(){
for(int i=0; i < 5; i++){
System.out.println(“Child Thread : ” + i);
try{
Thread.sleep(50);
}
catch(InterruptedException ie){
System.out.println(“Child thread interrupted! ” + ie);
}
}
System.out.println(“Child thread finished!”);
}
public static void main(String[] args) {
/*
* To create new thread, use
* Thread(Runnable thread, String threadName)
* constructor.
*
*/
Thread t = new Thread(new CreateThreadRunnableExample(), “My Thread”);
/*
* To start a particular thread, use
* void start() method of Thread class.
*
* Please note that, after creation of a thread it will not start
* running until we call start method.
*/
t.start();
for(int i=0; i < 5; i++){
System.out.println(“Main thread : ” + i);
try{
Thread.sleep(100);
}
catch(InterruptedException ie){
System.out.println(“Child thread interrupted! ” + ie);
}
}
System.out.println(“Main thread finished!”);
}
}
/*
Typical output of this thread example would be
Main thread : 0
Child Thread : 0
Child Thread : 1
Main thread : 1
Main thread : 2
Child Thread : 2
Child Thread : 3
Main thread : 3
Main thread : 4
Child Thread : 4
Child thread finished!
Main thread finished!
*/
this is the right output for above program
Main thread : 0
Child Thread : 0
Child Thread : 1
Main thread : 1
Child Thread : 2
Child Thread : 3
Main thread : 2
Child Thread : 4
Child thread finished!
Main thread : 3
Main thread : 4
Main thread finished!
for the above program the output what you have given is incorrect.
the actual output is:
———————-
Main thread : 0
Child Thread : 0
Child Thread : 1
Main thread : 1
Child Thread : 2
Child Thread : 3
Main thread : 2
Child Thread : 4
Child thread finished!
Main thread : 3
Main thread : 4
Main thread finished!
the output of the above will be right when Thread.sleep value in the run method will be 100 please correct it…….whatever its great program. Nice for basic …..i loved your programs:-)
I believe that “thread.sleep” actually post-pones the program by a time specified in milliseconds.
Actually, there is no correct output as it depends on your processor speed and configuration