• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Synchronizing thread

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everybody,

I'm facing one problem in below code from K&B Thread exercise Ques No. 2

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:

� 3 E and F are correct. E and F both cause both threads to lock on the same object, which will

prevent the threads from running simultaneously, and guarantee XXYY or YYXX. It's a bit

unusual to lock on an object like System.out, but it's perfectly legal, and both threads are

locking on the same object.

�˚ A can't guarantee anything since it has no synchronization. B and D both synchronize on

an instance of the Letters class�but since there are two different instances in the main()

method, the two threads do not block each other and may run simultaneously, resulting in

output like XYXY. C won't compile because it tries to override run() with a static method,

and also calls a non-static method from a static method. G won't compile because

System.out.class is nonsense. A class literal must start with a class name. System.out is

a field not a class, so System.out.class is not a valid class literal.


In the above code I'm not able to understand that how we can restrict output to XXYY by synchronizing through System.out

Regards
Manju
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
System.out is an object which represents the console where every output is displayed. As far as I know there is only one of this object in one JVM. So it is shared by all the classes and methods. So synchronizing on it will let only one of the synchronized code to run at a time...
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clarify your doubt, have a look at the following statement from the System class:



The *out* variable is a static variable in the System class. Hence only one lock is available for all the other Threads to lock upon.

Therefore making F a valid option.
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic