aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized(this) 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized(this)" Watch "synchronized(this)" New topic
Author

synchronized(this)

E. Justice
Greenhorn

Joined: Jun 20, 2007
Posts: 12
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.


SCJP 1.4
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
What did you expected? Also it would be better if you post the complete code.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi,

what did you expect as output? It may be helpful if you post your test program.
E. Justice
Greenhorn

Joined: Jun 20, 2007
Posts: 12
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

Joined: Aug 03, 2002
Posts: 7729
(How can I format the indentations correctly?)

By using tags to enclose your nicely indented code.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
E. Justice
Greenhorn

Joined: Jun 20, 2007
Posts: 12
Thank you Barry
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
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.


SCJP 5.0, SCWCD in progress
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
 
 
subject: synchronized(this)