• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized(this)

 
E. Justice
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I'm having a hard time understanding the concept of synchronization. I'm using the K&B SCJP 5.0 book and re-studying everything and today I'm looking at chapter 9 threads.

One topic I can't figure out is the synchronized block (page 708):

class SyncTest{
public void doStuff(){
System.out.println("not synchronized");
synchronized(this){
System.out.println("synchronized");
}
}
}

I've tried using a block of code like that in test programs however, every time it goes through the block it prints out both statements no matter where I call doStuff() from.

Thank you in advance for all the help.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you expected? Also it would be better if you post the complete code.
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what did you expect as output? It may be helpful if you post your test program.
 
E. Justice
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my code:

Sorry for the poor formating (How can I format the indentations correctly?)

---START CODE---

---END CODE---

The output I get is:

---START OUTPUT---

Not Sync
Sync
In the run()
Not Sync
Sync
X=: Ron 0
X=: Ron 1
X=: Ron 2
Not Sync
Sync
X=: Ron 3
Not Sync
Sync
In the run()
Not Sync
Sync
X=: Rick 0
X=: Rick 1
X=: Rick 2
Not Sync
Sync
X=: Rick 3
Not Sync
Sync

---END OUTPUT---

I expected for the first call to doStuff to only print out 'Not Sync' because it was not called from a synchronized context and since the rest were I was expecting the 'Sync' output.

[ June 25, 2007: Message edited by: E. Justice ]
[ June 25, 2007: Message edited by: Barry Gaunt ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(How can I format the indentations correctly?)

By using tags to enclose your nicely indented code.
 
E. Justice
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Barry
 
Mohit Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
w.r.t (I expected for the first call to doStuff to only print out 'Not Sync' because it was not called from a synchronized context..)

The first call to doStuff i.e. st.doStuff(); (line 5 in above code) is made by the "main" thread which executes the main() method.
Since no other thread acquired the lock on object "st" yet, main thread can execute the code written within "synchronized(this)" and hence 'Sync' is printed too.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy ranchers!

E. Justice wrote:
Sorry for the poor formating (How can I format the indentations correctly?)

Thanks for asking.

To indent your code properly:

1) At the posting page, mark the part of your posting that should be indented. Usually that will be your code (or some tabellaric output).

2) Hit the - Button below.

that's it!



About your problem:
When a first thread enters the synchronized block, it get's the lock of the object the synch is on. In your example this can be an object of type MyRun - for it has got its run method synchronized. Or it can be an object of type SyncTest, as it has a synch block.
When a second thread comes to the synchronized block, it cannot enter the sync block, as the first thread has the lock. The second thread will shift from runnable to waiting/blocking until the first thread returns the lock. And it will return the lock only when it leaves the sync block.

In your example the second thread (i.e. mt2) really has the chance to perform the first line an print "not sync" and then wait, but this doesn't happen.
Simply because just printin "sync" after is such a short task. So you never see a stop here.

Perhaps add a wait in the sync block of class SyncTest.



Second, you should name your thread before you start it. Otherwise it might output its default name, before it has been updated.
You can also name a thread by using its constructor. What about trying this:


One possible output:
main ready
Thread t1 in run method just before the synch block
Thread t1 ENTERED the synch block and sleeps
Thread t2 in run method just before the synch block
Thread t1 leaves the synch block
Thread t2 ENTERED the synch block and sleeps
Thread t1 AFTER the synch block, method ends.
Thread t2 leaves the synch block
Thread t2 AFTER the synch block, method ends.

The bold line shows, where the second thread is waiting for t1 leaving the sync block.


Yours,
Bu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic