• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

K&C book Threads doubt in Exercise question#2

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rashid Mian
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic