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...
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
posted
0
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
[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
posted
0
[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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.