File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please quote the source of the question...
 
Abhi vijay
Ranch Hand
Posts: 509
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K B book
 
Ankit Garg
Sheriff
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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???
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot HarvindEr and Ankit.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic