Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about Thread(ch09 q15)

 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


And given these two fragments.
I. synchronized void chat(long id) {
II. void chat(long id) {

when fragment I or fragment II is inserted at line 5, which are true? (Choose all that apply.)
A. An exception is thrown at runtime
B. With fragment I, compilation fails
C. With fragment II, compilation fails
D. With fragment I, the output could be yo dude dude yo
E. With fragment I, the output could be dude dude yo yo
F. With fragment II, the output could be yo dude dude yo

Answer:
F is correct. With fragment I, the chat method is synchronized, so the two threads can't swap back and forth. With either fragment, the first output must be yo.

I know the answer is F.
What I don't understand is the explanation.
Why "the first output must be yo with either fragment.
I think if it is the fragment II, it is very possible that the output is dude dude yo yo.
Though I couldn't get the output with dude being the first,
but if I changed method chat a little

I got the output dude dude yo yo

Is the explanation wrong, or I misunderstood the question?
 
matt freake
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zheng li wrote:

I know the answer is F.
What I don't understand is the explanation.
Why "the first output must be yo with either fragment.
I think if it is the fragment II, it is very possible that the output is dude dude yo yo.


I wondered this too. In particular I wondered, is there any guarantee in Java that two threads will run at the same speed. I could not find such a guarantee. If they don't it is possible that once the first thread has set "flag" equal to it's thread id, the second thread could overtake the first and reach the System.out.println. Because for the second thread, flag and id would be different, it would print 'dude' first.

Highly unlikely, I'm sure, but without that guarantee, I would still think it possible.

 
Mo Jay
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
There is NO way that dude will printed first in this code because you have 2 threads that are NOT interfering with each other because each one of them has his own lock. They are NOT acquiring the same lock on the same object, therefore each one of them is executing chat() method individually from top to bottom.

In order to have dude printed first you need to lock both threads on the same object, and below is an example of the code snippet that will do it:
DudesChat dc = new DudesChat();
Thread t1 = new Thread(dc);
Thread t2 = new Thread(dc);

t1.start();
t2.start();

Hope this helps clarify the issue.

Cheers!!!
 
zheng li
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mo Jay wrote:Hi Guys,
There is NO way that dude will printed first in this code because you have 2 threads that are NOT interfering with each other because each one of them has his own lock. They are NOT acquiring the same lock on the same object, therefore each one of them is executing chat() method individually from top to bottom.

In order to have dude printed first you need to lock both threads on the same object, and below is an example of the code snippet that will do it:
DudesChat dc = new DudesChat();
Thread t1 = new Thread(dc);
Thread t2 = new Thread(dc);

t1.start();
t2.start();

Hope this helps clarify the issue.

Cheers!!!


I know there are two objects, but flag is declared static, so the 2 threads still have chance to interfere each other.
 
matt freake
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mo Jay wrote:Hi Guys,
There is NO way that dude will printed first in this code because you have 2 threads that are NOT interfering with each other because each one of them has his own lock. They are NOT acquiring the same lock on the same object, therefore each one of them is executing chat() method individually from top to bottom.


Sorry, I was not clear, I was talking about the non synchronized version. Surely in that case it is possible (but unlikely) that Dude would be printed first.

If you imagine the two threads [T1] and [T2], running concurrently and interleaving, with time progressing as we move down the page

[T1]if (flag == 0) flag = id; // flag == 0, so flag is set to 1234
[T2]if (flag ==0) flag = id; // flag == 1234, so 'if' is false
[T2]// AT THIS POINT, THREAD 2 'OVERTAKES' THREAD 1 AND REACHES THE SYSTEM.OUT.PRINTLN FIRST
[T2]for (int x = 1; x < 3; x ++) {
[T2]if (flag == id) System.out.print("yo ");
[T2]// flag (1234) != id (9999) so this is false
[T2]else System.out.print("dude ");
[T2]// SO WE OUTPUT 'dude' HERE, FIRST!
[T1]for (int x = 1; x < 3; x ++) {
[T1]if (flag == id) System.out.print("yo ");
...



I may be missing something but I can't see why that is not possible if java does not guarantee that threads run at equal speeds.







Thanks.
 
Paul Mynhart
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's enough to slow down process T1 (see post of matt freake) to really obtain dude dude yo yo when running
DudesChat:

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul : Oh! you joined in 2003 and you remember[or you should be a regular visitor] the login password and after 7 year this is your first post!

really, I surprised . I am glad to say Welcome to JavaRanch
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question discussed recently here and here
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic