| Author |
Question on thread q.2 pg 734
|
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
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.
|
Thanks<br />Dinesh
|
 |
Bert Bates
author
Sheriff
Joined: Oct 14, 2002
Posts: 8712
|
|
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
|
Eliminate fossil fuel subsidies. (If you're not on the edge, you're taking up too much room.)
|
 |
Dinesh Tahiliani
Ranch Hand
Joined: Aug 06, 2007
Posts: 486
|
|
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
|
 |
 |
|
|
subject: Question on thread q.2 pg 734
|
|
|