• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Thread-Synchronized ?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
get schwifty. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic