Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronization: whats the answer

 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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???
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
i didn't understand how can we get the output 4 2 4 2
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic