This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes synchronization: whats the answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "synchronization: whats the answer" Watch "synchronization: whats the answer" New topic
Author

synchronization: whats the answer

Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
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

Joined: Nov 04, 2009
Posts: 2066

=> UseCodeTags <=, when you post code snaps! It will get quick responses! Which part, you don't get understand? => ShowSomeEffort <=


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
i didn't understand how can we get the output 4 2 4 2
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4165
    
  21

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.


Steve
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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

Joined: May 17, 2010
Posts: 65
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

Joined: Nov 04, 2009
Posts: 2066

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

Joined: May 17, 2010
Posts: 65
yaa...okay i got that...as there are two different objects of the chess class...two different threads can access the synchronized method...thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronization: whats the answer
 
Similar Threads
Synchronization
synchronized
[Thread] Synchronized - Object Locks
Synchronization and Locks
Doubt in Threads