aspose file tools*
The moose likes Threads and Synchronization and the fly likes Synchronized blocks Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Synchronized blocks" Watch "Synchronized blocks" New topic
Author

Synchronized blocks

Sonia Ballesteros Rey
Greenhorn

Joined: Apr 05, 2012
Posts: 9

Hello everybody,
I am practising with threads, and I had understood that if you have got a synchronized block, only one thread at time can have the lock and until this is released the other threads can access the block (or something like that, my english is not very fluent). So, I did this little code



but the output is not what I was expecting. I thought that one thread would finish before the other begin, but I have always this output:
Thread-1
9
8
7
6
5
4
3
Thread-3
9
8
7
6
5
4
3
2
1
0
2
1
0
I have been all the afternoon thinking about this, and reading the Java Tutorials, my books and Google, but I cannot get what is wrong with the code.
Thank you.


Lo divertido es el viaje, no el llegar.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

The important thing about synchronization is that threads synchronize on an object. Two threads synchronized on the same object cannot run at the same time.

In your case, you have synchronized(this) - so on the CountDown object. Your two threads have separate CountDown objects, so they don't have a problem running at the same time.

Try changing it to synchronized(CountDown.class), and see what the effect is.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Matthew Brown wrote:
Try changing it to synchronized(CountDown.class), and see what the effect is.


Note, however, that we don't normally sync on a Class object. In this case, it's just the easiest way to demonstrate the principle with as little change as possible to your existing code.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

No, the synchronization as you've written it is done is on the object, not the whole class. You create two instances of CountDown, so there's no synchronization between the two. If you created two threads that accessed the same instance of CountDown, then you would see that "finish before the next one starts" behavior that you expect.

ETA: I got interrupted halfway through typing that so when I submitted, the answer had already been given and more clearly than mine. Arrgh!
Sonia Ballesteros Rey
Greenhorn

Joined: Apr 05, 2012
Posts: 9

Wow, those were fast answers, thank you very much, Matthew and Jeff.
Well, I have tried what you said



but the output is the same. So, what next?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sonia Ballesteros Rey wrote:
but the output is the same. So, what next?


You must have forgotten to recompile. That new code cannot produce your original output.
Sonia Ballesteros Rey
Greenhorn

Joined: Apr 05, 2012
Posts: 9

Sorry, Greg, I was writing when you send your answer.
I have tried this too


and then the output is
Thread-1
9
8
7
6
5
4
3
Thread-2
2
1
0
I suppose this is just a silly problem, but really, what a pain in the ass.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Greg Charles wrote:No, the synchronization as you've written it is done is on the object, not the whole class.


Well, to be picky, all syncing is done on an object. There's no such thing as syncing on a "whole class".
Sonia Ballesteros Rey
Greenhorn

Joined: Apr 05, 2012
Posts: 9

Nope, Jeff, sorry. I have not forgotten to recompile (I went to Recompile 101 )
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


It looks fine to me... and from your last post -- which I assume is incomplete -- it looks fine as well. Keep in mind that you only synchronized the loop, so it means that the two sets of countdown numbers should not interlace. The "Thread" printout part isn't synchronized and can appear as it is in the middle of the other threads loop.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sonia Ballesteros Rey wrote:Nope, Jeff, sorry. I have not forgotten to recompile (I went to Recompile 101 )


This code, that you posted previously:



cannot produce interleaved counts.

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Sonia Ballesteros Rey wrote:
and then the output is
Thread-1
9
8
7
6
5
4
3
Thread-2
2
1
0
I suppose this is just a silly problem, but really, what a pain in the ass.

Ah, well, there are two important points there.

1. You now have both threads sharing the same CountDown object. So they share the same state. It now makes no difference whether you synchronize on the object or the class - one will count down to zero, and then the other will find the count is already at zero so will do nothing.

2. The one bit of interleaving you see - "Thread-2" is printed by line 14, which is before the threads enter the synchronized block. So you can tell that Thread 2 gets the chance to run at that point, but will immediately be blocked as soon as it enters counter().
Sonia Ballesteros Rey
Greenhorn

Joined: Apr 05, 2012
Posts: 9

Excuse me, Jeff. I thouhgt that you mean I haven´t recompiled the other code, with two threads and one object (too many answers at the same time for me). The code that you say gave me the impression of being wrong because it made what Henry said (thank you, Henry). So, I take away the println Thread this way


and now the output is
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0
Hurrah!!!
And more important, I know finally what was the problem. Thank you very much to everybody.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sonia Ballesteros Rey wrote:
and now the output is
9
8
7
6
5
4
3
2
1
0
9
8
7
6
5
4
3
2
1
0


But that doesn't prove that the threads are not interleaving. (They're not, but that output doesn't prove it.) It could be that T1 printed the first 9, and then T2 printed the 8 and 7, and then T1 the 6, and so on. It didn't happen that way, but we can't tell that from the output. You should have included the Thread name or something in each println() so you could see which thread it was.

That point aside though, I'm glad you got your problem solved.
Sonia Ballesteros Rey
Greenhorn

Joined: Apr 05, 2012
Posts: 9

Thank you, very much, Jeff.
Actually, I did what you suggest after my last post (then I had to stop, it´s a little late here) and I think that I will be making changes in the code for a while, seeing what happens to it.
Hey, and I´ve learnt a new word: interleaving.

Sonia.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Synchronized blocks