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(); [Link]("\tFrom Thread A : i="+i); } [Link]("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { [Link]("\tFrom Thread B : j="+j); //if(j==3) //stop(); } [Link]("Exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { [Link]("\tFrom Thread C : k="+k); /*if(k==1) try { sleep(1000); } catch(Exception e) { }*/ } [Link]("Exit from C"); } } class T
{ public static void main(String args[]) { A a=new A(); B b=new B(); C c=new C(); [Link]("Start thread A") ; [Link](); [Link]("Start thread B"); [Link](); [Link]("Start from C"); [Link](); [Link]("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(); [Link]("\tFrom Thread A : i="+i); } [Link]("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { [Link]("\tFrom Thread B : j="+j); if(j==3) stop(); } [Link]("Exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { [Link]("\tFrom Thread C : k="+k); if(k==1) try { sleep(1000); } catch(Exception e) { } } [Link]("Exit from C"); } } class T {
public static void main(String args[]) { A a=new A(); B b=new B(); C c=new C(); [Link]("Start thread A") ; [Link](); [Link]("Start thread B"); [Link](); [Link]("Start from C"); [Link](); [Link]("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(); [Link]("\tFrom Thread A : i="+i); } [Link]("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { [Link]("\tFrom Thread B : j="+j); //if(j==3) //stop(); } [Link]("Exit from B"); } } class C extends Thread { public void run() { for(int k=1;k<=5;k++) { [Link]("\tFrom Thread C : k="+k); /*if(k==1) try { sleep(1000); } catch(Exception e) { }*/ } [Link]("Exit from C"); } }
class T { public static void main(String args[]) { A a=new A(); B b=new B(); C c=new C(); [Link](1); [Link](5); [Link](10); [Link]("Start thread A") ; [Link](); [Link]("Start thread B"); [Link](); [Link]("Start from C"); [Link](); [Link]("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)