aspose file tools*
The moose likes Beginning Java and the fly likes synchronized is not working. why ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "synchronized is not working. why ? " Watch "synchronized is not working. why ? " New topic
Author

synchronized is not working. why ?

Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
Q. Here we have a CallMe class which have a call method,another class Caller implementing Runnable interface , it has a String variable.In main class three Threads have been created & initialised to different strings, now the run method creates objects of CallMe class & call method -> call which is synchronized but it is not working in a synchronized way.As the output shows.


class CallMe {
Synchronized void call(String s) {
System.out.println("["+s);
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
System.out.println(e);
}
System.out.println("]");
}
}
class Caller implements Runnable {
String str;

Caller(String s) {
str = s;
}

public void run() {
new CallMe().call(str);
}
}

class Pg240 {
public static void main(String args[]) {

CallMe cm = new CallMe();

Caller ob1 = new Caller("AMIT SETHI");
Caller ob2 = new Caller("HARISH SETHI");
Caller ob3 = new Caller("HEMANT SETHI");

Thread t1 = new Thread(ob1);
Thread t2 = new Thread(ob2);
Thread t3 = new Thread(ob3);

t1.start();
t2.start();
t3.start();

try {
t1.join();
t2.join();
t3.join();
}
catch(InterruptedException e) {
System.out.println("main interrupted");
}
}
}



output :without synchronised:

F:\j5se\cr>javac Pg240.java

F:\j5se\cr>java Pg240
[AMIT SETHI
[HARISH SETHI
[HEMANT SETHI
]
]
]


output :with synchronised: (both are same why ?)

F:\j5se\cr>javac Pg240.java

F:\j5se\cr>java Pg240
[AMIT SETHI
[HARISH SETHI
[HEMANT SETHI
]
]
]

pls help me .
Thanks in advance.
regards
Amit Sethi
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
What do you expect to happen? You have three different threads using three different CallMe instances. Synchronization doesn't even enter into the picture, you do not have in any place more than one method executing the same code on the same instance.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

I think you misunderstand how synchronization works in Java.

Synchronization works with locks. Inside a synchronized method or block, the lock of the object you are synchronizing on is locked. You can only enter a synchronized method or block if the lock is unlocked (which means that no other thread is executing the method or block using the same object).

Note, the "using the same object" is important to understand here.

You have three different Caller objects. Each of those has its own lock. If you lock the lock of ob1, the locks of ob2 and ob3 are still unlocked, and you can still call the call() method on ob2 and ob3 without having to wait for the lock.

Try changing your code like this and you'll see that it will work:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Amit Sethi
Ranch Hand

Joined: May 30, 2006
Posts: 45
Fst of all thanks gyus , I understood the concept now.

Q. But how this code working fine ? (code from complete refrence j2se 5 edition Page no.239.


class Callme {
synchronized void call(String msg) {
System.out.println("["+msg);
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
System.out.println(e);
}
System.out.println("]");
}
}

class Caller implements Runnable {
String msg;
Thread t;
Callme target;

public Caller(Callme trag,String s) {
target = trag;
msg = s;
t = new Thread(this);
t.start();
}

public void run() {
target.call(msg);
}
}

class Pg239_2 {
public static void main(String args[]) {
Callme target = new Callme();
Caller ob1 = new Caller(target,"hello");
Caller ob2 = new Caller(target,"synchronized");
Caller ob3 = new Caller(target,"world");

try {
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e) {
System.out.println(e);
}
System.out.println("main exit now");
}


output:
F:\j5se\cr>javac Pg239_2.java

F:\j5se\cr>java Pg239_2
[hello
]
[synchronized
]
[world
]
main exit now


pls help me.
regards
Amit Sethi
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
All of the "Caller" instances are using the same "Callme" instance. As such one "Caller" cannot execute call(String) on the Callme instance until it acquires the monitor for that instance. So the first thread to execute call(msg) acquires the monitor, executes the code, then leaves the call() method and releases the monitor. At this point the remaining to threads which have been blocked and waiting on that monitor compete to acquire it. One of them does and it then executes the call() method. Once it finishes it releases the monitor and that allows the third and final thread, which is still waiting, to acquire the monitor and complete call() itself. Basically, only one thread can execute call() on any given Callme instance at a time. That's any given instance, since they're all executing on the same instance that means only one of them can execute it at a time. In your case you had three different instances of Callme so they did not conflict.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized is not working. why ?
 
Similar Threads
HAS-A Relationship
Basics of Thread and Synchronization
Method overriding (K & B Pg.no. 104)
Doubts on output of Thread Synchronization
thread's