package Io;
public class Synchronous
{
public static void main(String[] args)
{
final String LOCK=new String();
new Thread(new Runnable()
{
@Override
public void run()
{
synchronized (LOCK)
{
System.out.println("我来了");
LOCK.notifyAll();
}
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (LOCK)
{
System.out.println("我来了0005");
LOCK.notifyAll();
}
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(200);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (LOCK)
{
System.out.println("我来了002");
LOCK.notifyAll();
}
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
synchronized (LOCK)
{
System.out.println("我来了11111111111111");
LOCK.notifyAll();
}
}
}).start();
}
public class Synchronous
{
public static void main(String[] args)
{
final String LOCK=new String();
new Thread(new Runnable()
{
@Override
public void run()
{
synchronized (LOCK)
{
System.out.println("我来了");
LOCK.notifyAll();
}
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(1000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (LOCK)
{
System.out.println("我来了0005");
LOCK.notifyAll();
}
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
try
{
Thread.sleep(200);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (LOCK)
{
System.out.println("我来了002");
LOCK.notifyAll();
}
}
}).start();
new Thread(new Runnable()
{
@Override
public void run()
{
synchronized (LOCK)
{
System.out.println("我来了11111111111111");
LOCK.notifyAll();
}
}
}).start();
}
}
谁先拿到锁谁就先走,同步,要排队的,排队 排队 排队
结果是:
我来了
我来了11111111111111
我来了002
我来了0005
也有可能是:
我来了11111111111111
我来了
我来了002
我来了0005