Win a copy of Pipeline as Code this week in the Cloud/Virtualization 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Thread

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ThreadDemo
{


synchronized void a()
{
actBusy();
}

static synchronized void b()
{
actBusy();
}

static void actBusy()
{
try
{
System.out.println(Thread.currentThread().getName());
Thread.sleep(1000);

}
catch(Exception e)
{
}
}

public static void main(String[] args)

{
final ThreadDemo x = new ThreadDemo();
final ThreadDemo y = new ThreadDemo();
Runnable runnable = new Runnable()
{
public void run()
{
int option =(int) (Math.random()*4);
switch(option)
{
case 0: x.a();break;
case 1: x.b(); break;
case 2: y.a(); break;
case 3: y.b();break;
}
}
};


Thread t1 =new Thread(runnable,"One");
Thread t2 =new Thread(runnable,"Two");
t1.start();
t2.start();
}
}

The answer here is One
Two.

Can someone please explain me how this program works.I am getting confused with the static synchronized, non-static synchronized methods.
 
Sheriff
Posts: 9679
42
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
please quote the source of the question...
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
K B book
 
Ankit Garg
Sheriff
Posts: 9679
42
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
I think the order of One and Two are indeterminable here. Since method a and b synchronize on different objects (a on instance of ThreadDemo and b on ThreadDemo.class) so if any of them are invoked, they will not block each other. As far as I remember, the question was about which method cannot be invoked by both the methods concurrently. And the answer was something like

x.b() and x.b()
x.b() and y.b()
x.a() and x.a()

am I correct???
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The possible scenarios are:

case 0: x.a();break;
case 1: x.b(); break;
case 2: y.a(); break;
case 3: y.b();break;

Thread t1.run().option = 0 ==> x.a();
Thread t2.run().option = 0 ==> x.a();


The above would print "One Two or Two One" with one of the thread's blocking to acquire an instance level lock from the other. The same applies if the option were 2 in each thread.

Thread t1.run().option = 1 ==> x.b();
Thread t2.run().option = 1 ==> x.b();


The above would print "One Two or Two One" with one of the thread's blocking to acquire an class level lock from the other. The same applies if the option were 3 in each thread.

Thread t1.run().option = 1 ==> x.b();
Thread t2.run().option = 3 ==> y.b();


The above would print "One Two or Two One" with one of the thread's blocking to acquire an class level lock from the other. The same applies if the option were 3 in each thread.

For all other combinations, the output will be "One Two or Two One" and the threads would not block to acquire a lock from the other because both the threads would be holding different locks viz. class specific or instance specific lock. So x.a() and y.a() called from two different threads would not block as they are blocking on two different instance specific locks.

The order of output is not predictable as it is not guaranteed as to which thread will be first made a thread of execution by the scheduler.

Hope this helps.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot HarvindEr and Ankit.
 
Poop goes in a willow feeder. Wipe with this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic