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 Question on thread q.2 pg 734 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 "Question on thread q.2 pg 734" Watch "Question on thread q.2 pg 734" New topic
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: 8804
    
    5
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


Spot false dilemmas now, ask me how!
(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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question on thread q.2 pg 734
 
Similar Threads
K&C book Threads doubt in Exercise question#2
Synchronized block K&B problem
Synchronization
chapter 9 self-test Q.2
scjp self test on thread doubt