File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes about Thread(ch09 q15) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "about Thread(ch09 q15)" Watch "about Thread(ch09 q15)" New topic
Author

about Thread(ch09 q15)

zheng li
Ranch Hand

Joined: Jun 16, 2009
Posts: 56


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

Joined: Jun 30, 2009
Posts: 5
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

Joined: Feb 16, 2009
Posts: 83
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

Joined: Jun 16, 2009
Posts: 56
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

Joined: Jun 30, 2009
Posts: 5
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

Joined: Sep 14, 2003
Posts: 1
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

Joined: Jan 28, 2008
Posts: 5575

@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

Joined: Nov 04, 2009
Posts: 2066

This question discussed recently here and here


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: about Thread(ch09 q15)