This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Thread-Synchronized ?

 
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 ?
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
 
Bartender
Posts: 1952
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
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
 
Always look on the bright side of life. At least this ad is really tiny:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic