package a0524;
class SynLock implements Runnable {
private boolean flag;
Object locka=new Object();
Object lockb=new Object();
public SynLock(boolean flag)
{
this.flag=flag;
}
@Override
public void run() {
// TODO Auto-generated method stub
if(flag)
{
while(true){
synchronized (locka) {
System.out.println(Thread.currentThread().getName()+"...if .......locka....");
synchronized (lockb) {
System.out.println(Thread.currentThread().getName()+"...if .......lockb....");
}
}
}
}else
{
while(true){
synchronized (lockb) {
System.out.println(Thread.currentThread().getName()+"...else .......lockb....");
synchronized (locka) {
System.out.println(Thread.currentThread().getName()+"...else .......locka....");
}
}
}
}
}
}
public class SynLockDemo1 {
public static void main(String []args )
{
SynLock l1=new SynLock(true);
SynLock l2=new SynLock(false);
Thread t1 =new Thread(l1);
Thread t2 =new Thread(l2);
t1.start();
t2.start();
}
}
|
|