• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread-Synchronized ?

 
rutuparna andhare
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rutuparna andhare
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure that the checkbox "Disable BB Code in this message" under the edit window is unchecked, otherwise the code tags will not work.
 
Dragos Nica
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic