Hi everyone,
Only three days left. I urgently need you all to check my analysis of
thread behaviour as
many mock exams are inconsistent. Listed below are 5 scenarios. The code has been compiled and run for each program. Correct me where I am incorrect.
// MyRunnable.java
//
Testing the behaviour of concurrent threads at the mercy of the O/S.
// "Platform used in this test is the one that needs continuous rebooting."
public class MyRunnable implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
public static void main(
String[] args){
MyRunnable mr = new MyRunnable();
Thread T1 = new Thread(mr,"Thread One");
Thread T2 = new Thread(mr,"Thread Two");
Thread T3 = new Thread(mr,"Thread Three");
T1.start();
T2.start();
T3.start();
System.out.println("Thread Main");
}
}
/*Scenario above has no synchronization or yielding. All the threads are competing for CPU time with the main thread. Therefore the output cannot be determined. However when I run this program the "Main Thread" is always printed first. Is it safe to say that the main thread will execute first as the order of the rest cannot be determined. Or is this unpredictable with pre-emptive OS */
// MyRunnable1.java
public class MyRunnable1 implements Runnable{
public synchronized void run(){
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args){
MyRunnable1 mr1 = new MyRunnable1();
Thread T1 = new Thread(mr1,"Thread One");
Thread T2 = new Thread(mr1,"Thread Two");
Thread T3 = new Thread(mr1,"Thread Three");
T1.start();
T2.start();
T3.start();
System.out.println("Thread Main");
}
}
/*Scenario above includes synchronization of the run method. Output is:
"Thread Main" is first and with different permutations for the rests of the output.
Therefore can I safely say that the output cannot be determined for the programmer
threads. */
// MyRunnable2.java
// Testing the behaviour of concurrent threads using yield method.
// "Platform used in this test is the one that needs continuous rebooting."
public class MyRunnable2 implements Runnable{
public void run(){
Thread.yield();
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args){
MyRunnable2 mr2 = new MyRunnable2();
Thread T1 = new Thread(mr2,"Thread One");
Thread T2 = new Thread(mr2,"Thread Two");
Thread T3 = new Thread(mr2,"Thread Three");
T1.start();
T2.start();
T3.start();
System.out.println("Thread Main");
}
}
/*Scenario above calls yield() in the run method. Output is:
"Thread Main" is first and with different permutations for the rests of the output.
Therefore can I safely say that the output cannot be determined for the programmer
threads again as in Second scenario. */
// MyRunnable3.java
// Testing the behaviour of concurrent threads using join() method.
// "Platform used in this test is the one that needs continuous rebooting."
public class MyRunnable3 implements Runnable{
public synchronized void run(){
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args)throws InterruptedException{
MyRunnable3 mr3 = new MyRunnable3();
Thread T1 = new Thread(mr3,"Thread One");
Thread T2 = new Thread(mr3,"Thread Two");
Thread T3 = new Thread(mr3,"Thread Three");
T1.start();
T1.join();
T2.start();
T2.join();
T3.start();
T3.join();
System.out.println("Thread Main");
}
}
/*Scenario above calls join() in main method. Note run method is synchronized .
The call to join() follows start() of that thread.
Output is: "Thread One" "Thread Two" "Thread Three" "Thread Main"
as expected. */
// MyRunnable4.java
// Testing the behaviour of concurrent threads using join() method.
// "Platform used in this test is the one that needs continuous rebooting."
public class MyRunnable4 implements Runnable{
public synchronized void run(){
System.out.println(Thread.currentThread().getName());
}
public static void main(String[] args)throws InterruptedException{
MyRunnable4 mr4 = new MyRunnable4();
Thread T1 = new Thread(mr4,"Thread One");
Thread T2 = new Thread(mr4,"Thread Two");
Thread T3 = new Thread(mr4,"Thread Three");
T1.start();
T2.start();
T3.start();
T1.join();
T2.join();
T3.join();
System.out.println("Thread Main");
}
}
/*Scenario above calls join() for each thread after all threads have been started.
Output is: Different permutations for programmer threads first and then "Thread Main"
Therefore would it be safe to say that the order of the programmer threads cannot be determined and main will be last. */
regards,
Stephen