• 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

synchronization: whats the answer

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Chess implements Runnable
{
public void run()
{
move(Thread.currentThread().getId());
}
synchronized void move(long id)
{
System.out.println(id+" ");
System.out.println(id+" ");
}
public static void main(String args[])
{
Chess ch=new Chess();
new Thread(ch).start();
new Thread(new Chess()).start();
}
}


In the K&B book...the say the answer can be.....4 2 4 2...how???
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
=> UseCodeTags <=, when you post code snaps! It will get quick responses! Which part, you don't get understand? => ShowSomeEffort <=
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't understand how can we get the output 4 2 4 2
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First consider what would have to happen for the answer to be 4 2 4 2?

Then consider this method:


What is that method synchronizing on?

Then consider the main method and see why the answer may come out the way it does.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've synchronized two objects with two different threads. Actually, I couldn't understand in which part, you don't get understand!
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the current object i.e the object calling the method move is synchronized.....

my doubt was that if one thread has an id of 4 and other of 2....and the method is synchronized...both the same id's should be printed together...i mean 4 4 2 2 or 2 2 4 4..how can one thread interrupt another...and get the output 4 2 4 2
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohnish Khiani wrote:the current object i.e the object calling the method move is synchronized.....

my doubt was that if one thread has an id of 4 and other of 2....and the method is synchronized...both the same id's should be printed together...i mean 4 4 2 2 or 2 2 4 4..how can one thread interrupt another...and get the output 4 2 4 2



You have wrong idea about synchronization. synchronization is for objects, not for method. Two different thread can't enter a synchronized method on a single object. Here, you've two different object and different threads, no problem to enter the move method.
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yaa...okay i got that...as there are two different objects of the chess class...two different threads can access the synchronized method...thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic