This week's book giveaway is in the Raspberry Pi forum.
We're giving away four copies of Getting started with Java on the Raspberry Pi and have Frank DelPorte on-line!
See this thread for details.
Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Synchronization - K&B question

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a question about synchronization of statics. I'm a bit confused. The problem is the following (from K&B)

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

And the options are:
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(); } }

The answers in the book are E & F, which I could corroborate by running the code... But I'm confused about option E.
As far as I know, the expression synchronized(Letters.class) synchronizes the static methods of the class. But since the methoid write is not static, the way is it forbidden that it could be run by two different threads simultaneously?
Thanks in advance
XM
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have a question about synchronization of statics. I'm a bit confused. The problem is the following (from K&B)




And the options are:




What is the question? Sorry if I am missing something.

Thanks.
 
Marx Villegas
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is the question #2 from the self test section of the chapter 9.
Here it is:

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

XM
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
syncronized(classname.class) ---

if one thread enters the lock of the object of the class..no other thread can enter any other object of the same class..

------------------------------------------------------------------
Magesh.s
 
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As far as I know, the expression synchronized(Letters.class) synchronizes the static methods of the class. But since the methoid write is not static, the way is it forbidden that it could be run by two different threads simultaneously?



Static methods of a class, uses the class object. This means that the static methods of the Letter class uses the Letter.class object to synchronize. Just because static methods uses a particular object, it doesn't mean that no other method can use the same object.

So... it is perfectly valid to use the Letter.class object.

Henry
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Block of code Synchronized at Class level ensures that only one thread can execute the block of code to completion.
So E is correct
Further F is also correct
[ April 18, 2006: Message edited by: Shaliey G ]
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some one please explain me how the lock on System.out works here? Does that means for all the objects of that class, it will not be possible for 2 objects to call println method?

Thanks in advance
 
Shaliey Gowtham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
out is static and final reference variable in the System class.
So its at class level, hence there can be only one thread executing the code locked at class level to completion until the lock is released.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronizing System.out doesn't guarantee that the two System.out statements are executed atomically. We can have a combination where

1. Thread 1 acquires the lock on System.out, prints 'X'.
2. Thread 2 acquires the lock on System.out, prints 'Y'.
3. Thread 1 acquires the lock on System.out, prints 'X'.
2. Thread 2 again acquires the lock on System.out, prints 'Y'.

Which, clearly is not the desired combination for the output.

Any comments?
[ April 19, 2006: Message edited by: Phani Kumar ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why isn't option D valid here?

I thought that a lock on any object could be used in a synchronized block of code??

Thanks
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic