• 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

chapter 9 self-test Q.2

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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(); } }

Accoring to me answer should be D but they are saying option E and F.please help me.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new Letter("x")....create new object
new Letter("y").....create new object
but each object has its own lock.so by synchronizing (Letter.class) the object is accomplish task and then the seccond one started.
by synchronizing System.out(you have to remember that it is an object)so one thread ata time can access it.
 
sapana jain
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry,but i am not getting.please explain it again.......
 
vasu chowdary
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok....... your choice is
public void run()
{
synchronized(this)
{
}
}
but..you must remember each object has its own lock(single).
in question
new letter("x").....one object is created
new letter("y").....seccond object is created
how can you synchronize two objects?
so it is very rare case ...by using
public void run()
{
synchronized(System.out)...\\the thread own the lock on System.out object
{ so the other thread must eait to
} print by System.out.println()
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic