Java Tutorial
// Create a thread by implementing Runnable interface in java program class ChildThread implements Runnable { Thread thr; ChildThread() { thr = new Thread(this, "Child Thread Task"); System.out.println("Child thread: " + thr); //starts child thread here thr.start(); } public void run() { try { int i = 0; while( i <= 4) { System.out.println("Child thread i: " + i); Thread.sleep(600); i++; } } catch(InterruptedException ex) { System.out.println("Child thread is interrupted."); } System.out.println("Child thread is completed"); } } class ThreadTest { public static void main(String args[]) { // Creates the child thread new ChildThread(); try { int i = 0; while( i <= 4) { System.out.println("Main thread i: " + i); Thread.sleep(900); i++; } } catch(InterruptedException ex) { System.out.println("Main thread is interrupted."); } System.out.println("Main thread is completed"); } }Output:
$ javac ThreadTest.java $ java ThreadTest Child thread: Thread[Child Thread Task,5,main] Main thread i: 0 Child thread i: 0 Child thread i: 1 Main thread i: 1 Child thread i: 2 Main thread i: 2 Child thread i: 3 Child thread i: 4 Main thread i: 3 Child thread is completed Main thread i: 4 Main thread is completed
// Create a thread by extending Thread class in java program class ChildThread extends Thread { ChildThread() { super("Child Thread Task"); System.out.println("Child thread: " + this); //starts child thread here start(); } public void run() { try { int i = 0; while( i <= 4) { System.out.println("Child thread i: " + i); Thread.sleep(600); i++; } } catch(InterruptedException ex) { System.out.println("Child thread is interrupted."); } System.out.println("Child thread is completed"); } } class ExtendThreadTest { public static void main(String args[]) { // Creates the child thread new ChildThread(); try { int i = 0; while( i <= 4) { System.out.println("Main thread i: " + i); Thread.sleep(900); i++; } } catch(InterruptedException ex) { System.out.println("Main thread is interrupted."); } System.out.println("Main thread is completed"); } }Output:
Child thread: Thread[Child Thread Task,5,main] Main thread i: 0 Child thread i: 0 Child thread i: 1 Main thread i: 1 Child thread i: 2 Main thread i: 2 Child thread i: 3 Child thread i: 4 Main thread i: 3 Child thread is completed Main thread i: 4 Main thread is completed
// Multiple Threads creation in java program class ChildThread implements Runnable { Thread thr; String threadName; ChildThread(String name) { threadName = name; thr = new Thread(this, threadName); System.out.println(threadName + "- Child thread: " + thr); //starts child thread here thr.start(); } public void run() { try { int i = 0; while( i <= 4) { System.out.println(threadName + "- Child thread i: " + i); Thread.sleep(600); i++; } } catch(InterruptedException ex) { System.out.println(threadName + "- Child thread is interrupted."); } System.out.println(threadName + "- Child thread is completed"); } } class MultipleThreadTest { public static void main(String args[]) { // Creates the child thread new ChildThread("one"); new ChildThread("two"); try { int i = 0; while( i <= 4) { System.out.println("Main thread i: " + i); Thread.sleep(900); i++; } } catch(InterruptedException ex) { System.out.println("Main thread is interrupted."); } System.out.println("Main thread is completed"); } }Output:
$ javac MultipleThreadTest.java $ java MultipleThreadTest one- Child thread: Thread[one,5,main] two- Child thread: Thread[two,5,main] one- Child thread i: 0 Main thread i: 0 two- Child thread i: 0 one- Child thread i: 1 two- Child thread i: 1 Main thread i: 1 one- Child thread i: 2 two- Child thread i: 2 Main thread i: 2 one- Child thread i: 3 two- Child thread i: 3 one- Child thread i: 4 two- Child thread i: 4 Main thread i: 3 one- Child thread is completed two- Child thread is completed Main thread i: 4 Main thread is completed
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page