• 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

synchronized locker quesion

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
James Quinton
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.

But I have the feeling that may not mean what you think it means.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
reply
    Bookmark Topic Watch Topic
  • New Topic