aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread and String literal pool Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread and String literal pool" Watch "Thread and String literal pool" New topic
Author

Thread and String literal pool

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I got this question from Exam Lab Test 01.

The output :

Don't those methods be synchronized on the String Literal pool object x? Thanks in Advanced!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

Yes the synchronized blocks will synchronize on the String "x" from the pool. Do you have some doubts about the output?? Here run2 is called from a synchronized block, so the thread already holds the lock on the desired object when run2 is called...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

line 3 is causes the method to be synchronized on the object "x"
this means that no other thread can access the synchronized code till the lock is acquired
now in that method
we call the run2() method
and this method is also synchronized on the object "x"
now when t1 or t2 is started
only one of the thread will run the synchronized code
and also the method called from the synchronized block is synchronized on the same object "x" (due to the String literal pool)
hence the method run2 can be accessed by the object of which lock the thread has acquired and it can call run2 through run method
hence
the output should contain X and then X2 for any order of the threads that are started and start executing

SCJP 6 [86%] June 30th, 2010
If you find any post useful, click the "plus one" sign on the right
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread and String literal pool
 
Similar Threads
Thread synchronized problem
Synchronized
Threads with separate lock Problem
wait() and notifyAll()