Documente Academic
Documente Profesional
Documente Cultură
Java FAQ
Recent Topics
Register / Login
Author
here is the question one of my friend asked me: 1. 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() {
1/3
www.coderanch.com/t/226212/java-programmer-SCJP/certification/synchronized-lock-object-method
5/5/13
12. for(int w = 0;w < 10;w++) { 13. System.out.println(holdA + holdB[w] + "."); 14. } 15. } 16. } They say answer is : 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. ----------------But I read that lock is on object and you can not execute the same synchronized method of the SAME object... and as here there are two different objects of Thread, how they could be synchronized?? [This message has been edited by ravish kumar (edited October 15, 2001).]
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." C hetan Parekh
William Brogden Author and all-around good cowpoke Rancher Joined: Mar 22, 2000 Posts: 12235
There is one MyThread (bad name for demonstration) object with two competing Threads trying to execute the synchronized method in the one object. Therefore one Thread runs to completion before the other gets to start. Bill
www.coderanch.com/t/226212/java-programmer-SCJP/certification/synchronized-lock-object-method
2/3
5/5/13
Similar Threads Hi Thread doubt Threads Sun sample question. Thread Program Synchronized Thread
All times above are in your local time zone & format.T he current ranch time (not your local time) is May 05, 2013 00:23:57 .
www.coderanch.com/t/226212/java-programmer-SCJP/certification/synchronized-lock-object-method
3/3