fork download
  1. //package arash.blogger.example.thread;
  2. /**
  3.   * by Arash M. Dehghani
  4.   * arashmd.blogspot.com
  5.   */
  6. class Core {
  7. public static void main(String[] args) {
  8. Runnable r0,r1;//pointers to thread methods
  9. r0=new FirstIteration(); //init Runnable
  10. r1=new SecondIteration();
  11. Thread t0,t1;//Thread class is used for starting a thread (runnable instance)
  12. t0=new Thread(r0);//init thread object, but haven't started yet
  13. t1=new Thread(r1);
  14. t0.start();//start the thread simultaneously
  15. t1.start();
  16. System.out.print("Threads started, no surprise here!\n");
  17. }
  18. }
  19. class FirstIteration implements Runnable{
  20. @Override
  21. public void run() {//thread starts from here
  22. for(int i=0;i<20;i++){
  23. System.out.print("Hello from 1st. thread, iteration="+i+"\n");
  24. }
  25. }
  26. }
  27. class SecondIteration implements Runnable{
  28. @Override
  29. public void run() {
  30. for(int i=0;i<20;i++){
  31. System.out.print("who just called 2st. thread? iteration="+i+"\n");
  32. }
  33. }
  34. }
Success #stdin #stdout 0.07s 382208KB
stdin
Standard input is empty
stdout
Hello from 1st. thread, iteration=0
Hello from 1st. thread, iteration=1
Hello from 1st. thread, iteration=2
Hello from 1st. thread, iteration=3
Hello from 1st. thread, iteration=4
Hello from 1st. thread, iteration=5
Hello from 1st. thread, iteration=6
Hello from 1st. thread, iteration=7
Hello from 1st. thread, iteration=8
Hello from 1st. thread, iteration=9
Hello from 1st. thread, iteration=10
Hello from 1st. thread, iteration=11
Hello from 1st. thread, iteration=12
Hello from 1st. thread, iteration=13
Hello from 1st. thread, iteration=14
Hello from 1st. thread, iteration=15
Hello from 1st. thread, iteration=16
Hello from 1st. thread, iteration=17
Hello from 1st. thread, iteration=18
Hello from 1st. thread, iteration=19
Threads started, no surprise here!
who just called 2st. thread? iteration=0
who just called 2st. thread? iteration=1
who just called 2st. thread? iteration=2
who just called 2st. thread? iteration=3
who just called 2st. thread? iteration=4
who just called 2st. thread? iteration=5
who just called 2st. thread? iteration=6
who just called 2st. thread? iteration=7
who just called 2st. thread? iteration=8
who just called 2st. thread? iteration=9
who just called 2st. thread? iteration=10
who just called 2st. thread? iteration=11
who just called 2st. thread? iteration=12
who just called 2st. thread? iteration=13
who just called 2st. thread? iteration=14
who just called 2st. thread? iteration=15
who just called 2st. thread? iteration=16
who just called 2st. thread? iteration=17
who just called 2st. thread? iteration=18
who just called 2st. thread? iteration=19