• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Thread doubt

 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers.
From K&B book.
I have this:
Given:


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 is E and F.
Can any one explain me for answer E ?
Thanks !!!
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Choice E: public void run() {synchronized (Letters.class) { write();} }

By saying synchronised(Letters.class) you are getting the lock of the class Letters (Remember there is one and only 1 lock for any class)

So when you say new Letters("X").start();
The above run() method will be automaticall invoked, and obtain the lock of class "Letters" and this thread will only release the lock after completing the write() method, which guarantees printing XX or YY depending on the thread invoking, but never XY.

Correct me if i am wrong.
 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi priti,

Originally posted by priti manas duddilla:
Choice E: public void run() {synchronized (Letters.class) { write();} }

By saying synchronised(Letters.class) you are getting the lock of the class Letters (Remember there is one and only 1 lock for any class)

So when you say new Letters("X").start();
The above run() method will be automaticall invoked, and obtain the lock of class "Letters" and this thread will only release the lock after completing the write() method, which guarantees printing XX or YY depending on the thread invoking, but never XY.

Correct me if i am wrong.



You are right.

Regards,
Nik
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys !!!
By "each object has just one lock" i understand : any instance of an object has just one lock.[Correct me if i'am wrong].
And per my understand for class litteral, When i write Letters.class the compiler will find the instance that represents the class called Letters.

I think my problem is with synchronising on class literal.
How does it work ?
Thanks.
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it !!!.
I was misunderstanding the String literal's concept.
I find this on the Sun's site:

There is one Class object for each loaded class known to the Java runtime system.




The output will be the same. And it refer to the same object.

And returning to my doubt:
when i write
public void run() {synchronized (Letters.class) { write();}
I will get the lock for the class litteral which is the same for
new Letters("X") and new Letters("Y").

Thanks !!!
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
why not
B: public synchronized void run() {write();}
C: public void run() { synchronized(this){ write();}}
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are running two instances, so they will acquire different locks and not block each other.
 
This tiny ad is wafer thin:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic