This is from a mock exam from the sun site.
. public class MyThread implements Runnable {
2. private
String holdA = "This is ";
3. private int[] holdB = {1,2,3,4,5,6,7,8,9,10};
4.
5. public static void main(String args[]) {
6. MyThread z = new MyThread();
7. (new
Thread(z)).start();
8. (new Thread(z)).start();
9. }
10.
11. public synchronized void run() {
12. for(int w = 0;w < 10;w++) {
13. System.out.println(holdA + holdB[w] + ".");
14. }
15. }
16. }
D. Compilation succeeds and the program prints each value in the holdB array at the end of the "This is " line. Each value is printed two times before the program ends, and the values are not printed in sequential order.
E. Compilation succeeds and the program prints each value in the holdB array at the end of the "This is " line. Each value is printed in order from 1 to 10 and, after the value 10 prints, it starts printing the values 1 to 10 in order again.
My question is why cannot the answer be "D". Aren't thread implementation platform dependent, and you cannot predict which thread is being run at a particular time.
If they have the same priority (I assumed same priority (5) since priority has not been explicitly mentioned), even though the threads are synchronized, can you really predict that the 2nd thread will be run after the 1st thread is completely finished ?
Thanks.