fork download
  1. class A extends Thread
  2. {
  3. public void run()
  4. {
  5. for(int i=1;i<=6;i++)
  6. {
  7. if(i==1)
  8. yield();
  9. System.out.println("\t From thread A:i="+i);
  10. }
  11. System.out.println("Exit from A");
  12. }
  13. }
  14. class B extends Thread
  15. {
  16. public void run()
  17. {
  18. for(int j=1;j<=8;j++)
  19. {
  20. System.out.println("\t From thread B:j="+j);
  21. if(j==3)
  22. stop();
  23. }
  24. System.out.println("Exit from B");
  25. }
  26. }
  27. class C extends Thread
  28. {
  29. public void run()
  30. {
  31. for(int k=1;k<=5;k++)
  32. {
  33. System.out.println("\t from thread c:k="+k);
  34. if(k==1)
  35. try
  36. {
  37. sleep(1000);
  38. }
  39. catch(Exception e)
  40. {
  41. }
  42. }
  43. System.out.println("exit from C");
  44.  
  45. }
  46. }
  47. class ThredTest
  48. {
  49. public static void main(String args[])
  50. {
  51. A obja=new A();
  52. B objb=new B();
  53. C objc=new C();
  54. System.out.println("Start thread A");
  55.  
  56. obja.start();
  57. System.out.println("Start thread B");
  58.  
  59. objb.start();
  60. System.out.println("Start thread C");
  61. objc.start();
  62. System.out.println("end of main thread");
  63. }
  64. }
Success #stdin #stdout 0.14s 65860KB
stdin
Standard input is empty
stdout
Start thread A
Start thread B
Start thread C
end of main thread
	 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
	 From thread A:i=6
Exit from A
	 From thread B:j=1
	 From thread B:j=2
	 From thread B:j=3
	 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