• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

synchronized( this ) vs synchronized(MyClass.class)

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from k&b, 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(); } }

Ans is:
public void run() { synchronized(Letters.class) { write(); } }
public void run () { synchronized (System.out) { write (); } }

I would like to know why answer cannot include
public static synchronized void run() { write(); }
public void run() { synchronized(this) { write(); } }

I guess, public static synchronized void run() is not correct as we do not have static variable in the class, not sure though.
Any explanation appreciated.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would like to know why answer cannot include
public static synchronized void run() { write(); }
public void run() { synchronized(this) { write(); } }


For the first statement, it's Compiler error if you override a non- static method with static one, and vice versa.

Second statement wouldn't give the desired output, because it synchronize on the current instance 'this' and you have 2 threads that are created with different instances, thus will not block each other and could execute concurrently.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I will take SCJP1.4



Can this be used in 1.4 ?? Please .
 
Would anybody like some fudge? I made it an hour ago. And it goes well with a tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic