This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&C book  Threads doubt in Exercise question#2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&C book  Threads doubt in Exercise question#2" Watch "K&C book  Threads doubt in Exercise question#2" New topic
Author

K&C book Threads doubt in Exercise question#2

Rashid Mian
Ranch Hand

Joined: Feb 14, 2007
Posts: 31
Given:

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.)

public void run() { write(); }

public synchronized void run() { write(); }

public static synchronized void run() { write(); }

public void run() { synchronized(this) { write(); } }

public void run() { synchronized(Letters.class) { write(); } }

public void run () { synchronized (System.out) { write (); } }

public void run() { synchronized(System.out.class) { write(); } }
---------------------------------------------------
my understanding(and testing in my JVM) says that option D is also correct, though i am still doubtful about two answers named as E and F. At first i thought that only option D is correct. Can any body elaborate in detail
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,

Its pretty straight forward...

Thread t1 = new Thread(runnableInstance);
Thread t2 = new Thread(runnableInstance);

t1 is not equal to t2.

both are different thread Objects.

So in run method of t1 ---> if you synchronize on this ---> it will obtain lock on t1 object.

And in run method of t2 ---> if you synchronize on this ---> it will obtain lock on t2 object.

No problem for two threads run on t1,t2 Objects run in parallel.
Hence you can't promise XXYY behavior.

If you want that : Both threads(of t1,t2 Objects) shall try for Lock Unique Object(not different). in our case "System.out" and "Letters.class"(static lock will be unique) are Unique Objects.

Hope this is much clear.


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Given:

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(); } }

-------------------------------------------------------------------------

Answer E and F are only correct,
The main concern here: You are creating two instances of the thread class, so you can't make sure the intended output by making the code block synchronized on the "this". This assures that no two thread can access this block at a time. But there are two locks (two instances you are creating), both will be executing in its own section, no blocking to each other. So therefore to get the intended result you can insert the code inside the sync block "Letters.class" or "System.out" because both are static and there will be one and only one lock per application execution of the static object. So there will be only one key, a thread needs to grab that key before entering into "critical section". Other thread will be blocked.


cmbhatt


cmbhatt
Rashid Mian
Ranch Hand

Joined: Feb 14, 2007
Posts: 31
Thanks a lot.
I appreciate your attention to my problem.
Can any body send me Thread related questions, so that I can have tight grip on this topic.
[ March 20, 2007: Message edited by: Rashid Mian ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&C book Threads doubt in Exercise question#2
 
Similar Threads
Synchronization
chapter 9 self-test Q.2
synchronized( this ) vs synchronized(MyClass.class)
New question on K&B Chapter 9 Question 2
Question on thread q.2 pg 734