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

Question on thread q.2 pg 734

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Letters extends Thread {
private String name;
public Letters(String name) {
this.name = name;
}
public void write() {
System.out.print(name);
System.out.print(name);
}
public static void main(String[] args) {
new Letters("X").start();
new Letters("Y").start();
}
}
We want to guarantee that the output can be either XXYY or YYXX, but never XYXY or any other combination. Which of the following method definitions could be added to the Letters
class to make this guarantee? (Choose all that apply.)
A. public void run() { write(); }
B. public synchronized void run() { write(); }
C. public static synchronized void run() { write(); }
D. public void run() { synchronized(this) { write(); } }
E. public void run() { synchronized(Letters.class) { write(); } }
F. public void run() { synchronized(System.out) { write(); } }
G. public void run() { synchronized(System.out.class) { write(); } }

Ans -- E,F .
Can someone explain me.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dinesh -

I see that you're asking a lot of questions from the threads chapter of our book. I think it would be better to start with one question, tell us what you think is going on, and we'll start a conversation from there. It could be that you might have to go back and reread some parts of the chapter, but we can't really tell until you give us more info about what parts make sense to you and what parts are confusing.

Thanks, I'm going to shut down the other threads.

Bert
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bert,

Not only , there are many people who have on therads.
As therad is considered as of the imp topics and many questions are asked in exams, to clear my doubt that is why i am posting questions so that i can get clear picture .

Thanks
Dinesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic