This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads

 
Soum Sark
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Soum:
That is seem good strategy. Good luck on your exam.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Soum Sark
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron has the correct answer.And it can't be anything else
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic