aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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

Joined: Aug 03, 2008
Posts: 9319
    
  17

please quote the source of the question...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
K B book
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

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

Joined: Jun 10, 2008
Posts: 231
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.


thanks
Harvinder
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks a lot HarvindEr and Ankit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread