This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "threads" Watch "threads" New topic
Author

threads

Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
This is a JWhiz question

the answer is output cannot be determined as expected but since the strings are never changed the output would be
Me You
Me You
Me You
Me You
woudn't it ...given the threads do run..since the order will not matter ..do we take for granted that when we start two threads the output can never be determined even if it is said that no other thread runs at that time (though this particular question does not say that)?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Soum:
I would agree with your analysis that output is deterministic because both threads are printing identifical strings twice. Therefore, which string is printing which line is immaterial. Have you tried to contact the source of this question about their willingness to re-evaluate this question?
Thanks
Barkat
[ September 04, 2002: Message edited by: Barkat Mardhani ]
Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
well i don't have time to get back to them..i go for the exam tomorrow....so for the exam i would go for the 'indeterminate' option if they specifically don't mention anything about any other threads running or not running.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Soum:
That is seem good strategy. Good luck on your exam.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Since there is no synchronization, isn't it possible for one thread's System.out.println() call to be interrupted, allowing the other thread to run? If so, you could get
output like
Me Me You
You


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Soum Sark
Ranch Hand

Joined: May 31, 2002
Posts: 50
Hi Ron:
I think you might be correct..i am no expert in threads..but if we were to synchronize the method then would the answer have been any different?
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You'd need to synchronize on some object that is available to both threads. System.out would be a reasonable choice for this object.
duole
Greenhorn

Joined: Jul 01, 2002
Posts: 7
Why isn't the output "Me to Me You for You Me to Me You for You"? but "Me Me You You"?
Thanks
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Because Strings are immutable. The concat method returns a new String, which we would need to capture with:
String foo = "foo";
System.out.println(foo);
foo.concat("bar");
System.out.println(foo);
Would return:
foo
foo
Whilst:
String foo = "foo";
System.out.println(foo);
bar = foo.concat("bar");
System.out.println(bar);
Would return:
foo
foobar
Cheers, Neil
Bharat Keskar
Greenhorn

Joined: Aug 26, 2002
Posts: 11
Ron has the correct answer.And it can't be anything else
 
wood burning stoves
 
subject: threads
 
Similar Threads
Reg. Some Thread basics
Threads
Certification4career.com
J@Whiz question
a question about thread.