aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread-Synchronized ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread-Synchronized ?" Watch "Thread-Synchronized ?" New topic
Author

Thread-Synchronized ?

rutuparna andhare
Greenhorn

Joined: Oct 29, 2009
Posts: 14
[code]
public class test implements Runnable
{
public void run()
{
try
{
synchronized("a")
{
System.out.println("before-->"+Thread.currentThread().getName());
"a".wait(5000); // line 1
System.out.println("after-->"+Thread.currentThread().getName());
}
}
catch(InterruptedException ie){}
}

public static void main(String [] str)
{
test work=new test();
Thread t1=new Thread(work); t1.setName("t1");
Thread t2=new Thread(work); t2.setName("t2");

t1.start();
t2.start();
}

}
[/code]
according to my view i want to see answer like this
(
before t1 ---- minimum 5 sec-- after t1
before t2 ---- minimum 5 sec-- after t2
)

but output is like this-->
before--> t1 before-->t2 5sec wait after -->t1 after--> t2

question? as sync code allow one thread to inside
then why output is "before t1 before t2"
how both thread access sync code?


and please explain sync "a" "a".wait ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

rutuparna please UseCodeTags when you post a source code. Unformatted code is very hard to read so edit your post using button and then add [code] [/code] tags around your source code...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1779
    
    7

When you invoke java.lang.Object.wait() in a synchronized context (which is a pre-condition) the invoking Thread effectively relinquishes the object's intrinsic lock, which is why in this case the ouput for the two threads can be intermingled. Have a look at API documentation of java.lang.Object for more information.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
rutuparna andhare
Greenhorn

Joined: Oct 29, 2009
Posts: 14
Ankit Garg wrote:rutuparna please UseCodeTags when you post a source code. Unformatted code is very hard to read so edit your post using button and then add [code] [/code] tags around your source code...



[code].........[/code]
but not getting formated source
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14273
    
  21

Make sure that the checkbox "Disable BB Code in this message" under the edit window is unchecked, otherwise the code tags will not work.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
[quote=rutuparna andhare][code]
........
but output is like this-->
before--> t1 before-->t2 5sec wait after -->t1 after--> t2

question? as sync code allow one thread to inside
then why output is "before t1 before t2"
how both thread access sync code?

and please explain sync "a" "a".wait ?[/quote]

I think that the following happens:
- Thread t1 get the lock on object "a" (this is a string but it is still an object), then prints " before--> t1"
- When it call wait on object "a" it release the lock on object "a" and the lock is taken by the second thread t2 that will print "before--> t2"
- Thread t2 calls wait on object "a" and releases the lock.
As nobody calls notify , after 5 sec, thread t1 goes to runnable , and scheduler choose it an put in in running thus printing "-> t1 after"
The same happen with t2

I hope I understand corectly..
Dragos
Dragos Nica
Ranch Hand

Joined: Oct 25, 2009
Posts: 39
[quote=rutuparna andhare][code]
........
but output is like this-->
before--> t1 before-->t2 5sec wait after -->t1 after--> t2

question? as sync code allow one thread to inside
then why output is "before t1 before t2"
how both thread access sync code?

and please explain sync "a" "a".wait ?[/quote]

I think that the following happens:
- Thread t1 get the lock on object "a" (this is a string but it is still an object), then prints " before--> t1"
- When it call wait on object "a" it release the lock on object "a" and the lock is taken by the second thread t2 that will print "before--> t2"
- Thread t2 calls wait on object "a" and releases the lock.
As nobody calls notify , after 5 sec, thread t1 goes to runnable , and scheduler choose it an put in in running thus printing "-> t1 after"
The same happen with t2

I hope I understand corectly..
Dragos
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Thread-Synchronized ?