0% found this document useful (0 votes)
25 views6 pages

Java Program To Implement Threads Input:w/o Conditions

The document discusses three Java programs that demonstrate threading concepts in Java. The first program creates three threads without any conditions. The second program adds yield(), sleep(), and stop() conditions. The third program demonstrates setting thread priorities. Each program outputs the order of execution to show the effects of the threading code.

Uploaded by

gnext19
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views6 pages

Java Program To Implement Threads Input:w/o Conditions

The document discusses three Java programs that demonstrate threading concepts in Java. The first program creates three threads without any conditions. The second program adds yield(), sleep(), and stop() conditions. The third program demonstrates setting thread priorities. Each program outputs the order of execution to show the effects of the threading code.

Uploaded by

gnext19
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

JAVA PROGRAM TO IMPLEMENT THREADS Input :w/o conditions class A extends Thread { public void run() { for(int i=1;i<=5;i++)

{ //if(i==1) //yield(); System.out.println("\tFrom Thread A : i="+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("\tFrom Thread B : j="+j); //if(j==3) //stop(); } System.out.println("Exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("\tFrom Thread C : k="+k); /*if(k==1) try { sleep(1000); } catch(Exception e) { }*/ } System.out.println("Exit from C"); } } class T

{ public static void main(String args[]) { A a=new A(); B b=new B(); C c=new C(); System.out.println("Start thread A") ; a.start(); System.out.println("Start thread B"); b.start(); System.out.println("Start from C"); c.start(); System.out.println("End of main thread"); } } Output : run: Start thread A Start thread B Start from C End of main thread From Thread B : j=1 From Thread B : j=2 From Thread B : j=3 From Thread B : j=4 From Thread B : j=5 Exit from B From Thread C : k=1 From Thread C : k=2 From Thread C : k=3 From Thread C : k=4 From Thread C : k=5 Exit from C From Thread A : i=1 From Thread A : i=2 From Thread A : i=3 From Thread A : i=4 From Thread A : i=5 Exit from A BUILD SUCCESSFUL (total time: 0 seconds)

Input with conditions class A extends Thread { public void run() { for(int i=1;i<=5;i++) { if(i==1) yield(); System.out.println("\tFrom Thread A : i="+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("\tFrom Thread B : j="+j); if(j==3) stop(); } System.out.println("Exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("\tFrom Thread C : k="+k); if(k==1) try { sleep(1000); } catch(Exception e) { } } System.out.println("Exit from C"); } } class T {

public static void main(String args[]) { A a=new A(); B b=new B(); C c=new C(); System.out.println("Start thread A") ; a.start(); System.out.println("Start thread B"); b.start(); System.out.println("Start from C"); c.start(); System.out.println("End of main thread"); } } Output: run: Start thread A Start thread B Start from C End of main thread From Thread B : j=1 From Thread B : j=2 From Thread B : j=3 From Thread A : i=1 From Thread A : i=2 From Thread A : i=3 From Thread A : i=4 From Thread A : i=5 Exit from A From Thread C : k=1 From Thread C : k=2 From Thread C : k=3 From Thread C : k=4 From Thread C : k=5 Exit from C BUILD SUCCESSFUL (total time: 1 second)

Input-setting priority class A extends Thread { public void run() { for(int i=1;i<=5;i++) { //if(i==1) //yield(); System.out.println("\tFrom Thread A : i="+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("\tFrom Thread B : j="+j); //if(j==3) //stop(); } System.out.println("Exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("\tFrom Thread C : k="+k); /*if(k==1) try { sleep(1000); } catch(Exception e) { }*/ } System.out.println("Exit from C"); } }

class T { public static void main(String args[]) { A a=new A(); B b=new B(); C c=new C(); a.setPriority(1); b.setPriority(5); c.setPriority(10); System.out.println("Start thread A") ; a.start(); System.out.println("Start thread B"); b.start(); System.out.println("Start from C"); c.start(); System.out.println("End of main thread"); } } Output: run: Start thread A Start thread B Start from C End of main thread From Thread C : k=1 From Thread C : k=2 From Thread C : k=3 From Thread C : k=4 From Thread C : k=5 Exit from C From Thread A : i=1 From Thread A : i=2 From Thread A : i=3 From Thread A : i=4 From Thread A : i=5 Exit from A From Thread B : j=1 From Thread B : j=2 From Thread B : j=3 From Thread B : j=4 From Thread B : j=5 Exit from B BUILD SUCCESSFUL (total time: 0 seconds)

You might also like