wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized locker quesion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized locker quesion" Watch "synchronized locker quesion" New topic
Author

synchronized locker quesion

James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
I found a mock question like this:

class MyThread extends Thread{
String s;
public void run(){
synchronized(s){
//something here
}
}
public MyThread(String s){
this.s=s;
}

public static void main(String[] args){
String s1="123";
String s2="123";
new MyThread(s1).start();
new MyThread(s2).start();
}
}

The answer of the question explains that two thread created will be synchronized and waiting each other on "synchronized" part because s1 and s2 are pointing to same string object the pool.

Is that true? Does that imply if s1 and s2 have different value such as:
String s1="123";
String s2="456";
The code will not be synchronized?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The code will be synchronized in either case, in the sense that the code uses synchronized blocks to acquire locks, as you can see from looking at the code. However if the two MyThread instances are synchronized on two different String instances (two different values of s) then the synchronization will have no significant effect - the two method calls may run concurrently. But if the two MyThread instances are synchronized on the same String instance, then the two run() methods may not run concurrently. Or more precisely, the synchronized blocks within the run() methods may not run concurrently.


"I'm not back." - Bill Harding, Twister
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
Originally posted by Jim Yingst:
The code will be synchronized in either case, in the sense that the code uses synchronized blocks to acquire locks, as you can see from looking at the code. However if the two MyThread instances are synchronized on two different String instances (two different values of s) then the synchronization will have no significant effect - the two method calls may run concurrently. But if the two MyThread instances are synchronized on the same String instance, then the two run() methods may not run concurrently. Or more precisely, the synchronized blocks within the run() methods may not run concurrently.

what are you talking about? I am totally out of mind
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, that doesn't give me much to work with; no idea where to begin. Have you tried the Java tutorial on snchronization?

[James]: The answer of the question explains that two thread created will be synchronized and waiting each other on "synchronized" part because s1 and s2 are pointing to same string object the pool.

Is that true?


Pretty much. Well they don't both wait for each other. One thread gets to the sync block first and starts executing it, no waiting. The other thread can't start the sync block until the first is done.

[James]: Does that imply if s1 and s2 have different value such as:
String s1="123";
String s2="456";
The code will not be synchronized?


No. The code will be synchronized, period, because the code uses the synchronized keyword - as you see in the code. That's just terminology. But what's important here is that the code is synchronized on a particular object (or two particular objects) known as the monitor - the object whose lock is acquired for synchronization.
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
Originally posted by Jim Yingst:
[James]: Does that imply if s1 and s2 have different value such as:
String s1="123";
String s2="456";
The code will not be synchronized?


No. The code will be synchronized, period, because the code uses the synchronized keyword - as you see in the code. That's just terminology. But what's important here is that the code is synchronized on a particular object (or two particular objects) known as the monitor - the object whose lock is acquired for synchronization.


Jim, we have two instances of MyThread class here, not just one.
I know what does "synchronized" mean on a particular object. But here we have reference s1 and s2 being used by two different threads. I guess the underlying question is: are s1 and s2 referring to one object?
[ November 20, 2006: Message edited by: James Quinton ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
In the code you just quoted, s1 and s2 refer to two different objects, "123" and "456". However in the original code, both s1 and s2 refer to the same object, "123". That's how Java handles String literals (and String constant expressions). Two literals / constant expressions that have the same content will be made to refer to the exact same instance. There are some very subtle exceptions involving multiple classloaders, but let's skip that for now.
James Quinton
Ranch Hand

Joined: Oct 02, 2006
Posts: 94
Originally posted by Jim Yingst:
In the code you just quoted, s1 and s2 refer to two different objects, "123" and "456". However in the original code, both s1 and s2 refer to the same object, "123". That's how Java handles String literals (and String constant expressions). Two literals / constant expressions that have the same content will be made to refer to the exact same instance. There are some very subtle exceptions involving multiple classloaders, but let's skip that for now.


ok, then. Let me put this way:
class MyThread extends Thread{
String s;
public void run(){
synchronized(s){
//something here
}
}
public MyThread(String s){
this.s=s;
}

public static void main(String[] args){
String s1="123";
String s2="456";
new MyThread(s1).start();
new MyThread(s2).start();
}
}

will the above "something here" will be synchronized?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes.

But I have the feeling that may not mean what you think it means.
KasiMurugan Ramasamy
Ranch Hand

Joined: Jan 30, 2005
Posts: 125
Hai,

The synchronized keyword is used to refer sychronization on a object, which are used by more than one thread.

Synchronization will take place when s1 and s2 value is 123.Since s1 and s2 are refering same object reference (from the string literal). That mean, the one thread can execute the synchronization block other has to wait.

The other s1 is 123 and s2 is 342. This case two objects are different, so both thread can execute the synchronization block simualtenously.


Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

James Quinton posted November 20, 2006 01:20 PM

The answer of the question explains that two thread created will be synchronized and waiting each other on "synchronized" part because s1 and s2 are pointing to same string object the pool.

Is that true?




Yes.
The following

produces
Thread-0
Thread-0
Thread-1
Thread-1

indication the synchronization.


and James asked also:
Does that imply if s1 and s2 have different value such as:
String s1="123";
String s2="456";
The code will not be synchronized?

Yes. Change the code as you described, and the code will produce
Thread-0
Thread-1
Thread-0
Thread-1




the yield() is only in so you can see the difference on synchronization.




Jim Yingst posted November 20, 2006 01:58 PM
The code will be synchronized in either case, (...)
This is wrong.


In the first case, the threads are synchronized on the same object (in the string pool), in the second case there are two different objects.


Yours,
Bu.


all events occur in real time
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
I posted some minutes ago

Jim Yingst posted November 20, 2006 01:58 PM

quote:
The code will be synchronized in either case, (...)

This is wrong.

This was not wrong.
Only a bit misleading. The code is synchronized in any case, only the threads are locking on the same object in the first case and on different objects in the second case.



Yours,
Bu.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Indeed, much as I said earlier. Merely asking "is it synchronized?" is useless here, which is why I went on to discuss the effect of synchronizing on one shared object or two distinct objects. It seems we're all repeating variations of the same thing here; I'm not sure which parts of this still require explanation. We seem to be going in circles...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronized locker quesion
 
Similar Threads
Can this be a case of deadlock ?And how to determine it output ?
Inner Thread Classes
more fun Qestions, thread=)
Deadlocks in Threads?
Thread