• 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

Question about thread synchronization

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fellow Ranchers,

I am getting quite stranded in threads over their synchrnozation. Please have a look at following code for my question-
<blockquote>code:
<pre name="code" class="core"> public class TestThisThread implements Runnable
{
Integer integer = new Integer(0);

public void run()
{
synchronized(System.out)
{
for (int i = 0; i < 10 ; i++ )
{
integer = i;
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName() + " : " + integer);
}
}
}

public static void main(String[] args)
{
TestThisThread t1 = new TestThisThread();
TestThisThread t2 = new TestThisThread();

new Thread(t1, "One").start();
new Thread(t2, "Two").start();
}
}
</pre>
</blockquote>

This code works perfectly fine printing all "One" threads output first and then "Two"'s.

Question is how it works like this when locked on System.out? If I change lock to be on this ouptputs will get mixed interweaving One's and Two's print statement.

Now, if I keep lock on 'this' only and start both threads with 't1' as runnable object, I will get nice output of either all One's first or all Two's first. This makes somewhat sense to me, as I am starting two threads with same runnable object, and locking on 'this' makes them properly synchronized. Please correct me if this is not correct assumption. :roll:
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
synchronized(System.out)
Here the thread which enters run method first will acquire lock on PrintStream object, and prevents other thread to acquire lock until it finishes.
[ July 17, 2008: Message edited by: ramesh maredu ]
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Question is how it works like this when locked on System.out? If I change lock to be on this ouptputs will get mixed interweaving One's and Two's print statement.



Right, because the 'this' object of thread t1 and t2 are different , they are not same.

Now, if I keep lock on 'this' only and start both threads with 't1' as runnable object, I will get nice output of either all One's first or all Two's first. This makes somewhat sense to me, as I am starting two threads with same runnable object, and locking on 'this' makes them properly synchronized. Please correct me if this is not correct assumption.



when you pass the same runnable object this, the thread gets locked on same object of "t1",
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Question is how it works like this when locked on System.out?



You need not to get lock on this object only to achieve synchronization, it can be on any object in this case it is PrintStream object
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here System is a class and out is the static object variable of this class having type of PrintStream.
I mean System.out is static lock.So there is only one lock for both the object t1 and t2.



But when you use 'this',it means lock of current object.t1 and t2 both will have there own lock for 'this'.

That is why in case 1 i.e. System.out,output is all one's than all two's but in case 2 i.e. 'this' lock output is unpredictable.

Now, if I keep lock on 'this' only and start both threads with 't1' as runnable object, I will get nice output of either all One's first or all Two's first.



I am confused with this statement.Please explain this.
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I mean System.out is static lock



can you please explain what is static lock,as far as i know we can acquire lock on object or on class.
 
Sumit Gaikaiwari
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for quick response, now I am getting it somewhat clearer.

So, can I summarize like this-
When I have threads started with different runnable objects, and still I want my synchronized code to run by only one thread at a time, I need to get lock on a class variable.

I tried with locking on 'TestThisThread.class' and output comes nicely in order according to my assumption.

In this scenario, it seems locking on 'this' can have undesirable effects. I mean if every thread is allowed to enter into synchronized code, does it signifies code is almost as if not synchronized?

Kamal, as per Sagar's explanation, when threads are started with same runnable object (in this case say, 't1') synchronizing on 'this' will be ok, as both threads will be having same 'this' i.e. t1 runnable object.
[ July 17, 2008: Message edited by: Sumit Gaikaiwari ]
 
kamal shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramesh,

Yes you are right.By static lock my mean was Class lock.

Sumit,

Yes,If we will use same t1 for both threads,lock on 'this' will work absolutely fine.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kamal shah:
Ramesh,

Yes you are right.By static lock my mean was Class lock.

Sumit,

Yes,If we will use same t1 for both threads,lock on 'this' will work absolutely fine.



By static lock you mean a class lock.
But just to make it clear, you aren't saying that a System.out is locking a class because its a static member, right? Because it's locking the object there in System.out.
If we wanted to lock a class we need to put a NameOfClass.class.

Maybe i got it wrong, corret me if i did.

Kind Regards,
Raphael Rabadan
[ July 18, 2008: Message edited by: Raphael Rabadan ]
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

By static lock you mean a class lock.
But just to make it clear, you aren't saying that a System.out is locking a class because its a static member, right? Because it's locking the object there in System.out.
If we wanted to lock a class we need to put a NameOfClass.class.



Yes, I agree with you Raphael, I think Kamal want to say it as "Static Lock", and not the class lock .
 
kamal shah
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,Guys..I was wrong in saying it as 'class lock',My mean was that it will behave exactly the same way as class lock do.

I do not know the exact word for this,so I used the static lock.
 
reply
    Bookmark Topic Watch Topic
  • New Topic