class Test11{
//主要测试Thread.sleep(100)不释放当前对象的锁,前提你要理解Thread.sleep()必然而且只能是当前线程调用的方法。
public static void main(String args[]){
class T{
public synchronized void a(){
try{
//long l = System.currentTimeMillis();
//System.out.println(l);
Thread.sleep(1000);
//wait(1000);
/*
while(System.currentTimeMillis()-l<1000){
System.out.println(1);
wait(System.currentTimeMillis()-l);
}*/
System.out.println("a");
}catch(Exception e){
}
}
public synchronized void b(){
try{
System.out.println("b");
}catch(Exception e){}
}
public void c(){
try{
Thread.sleep(10000);
System.out.println("c");
}catch(Exception e){}
}
public void d(){
try{
// Thread.sleep(10000);
System.out.println("d");
}catch(Exception e){}
}
}
class Tt extends Thread{
private T t;
public Tt(T t){
this.t = t;
}
public void run(){
this.t.a();
}
}
class Ttt extends Thread{
private T t;
public Ttt(T t){
this.t = t;
}
public void run(){
this.t.b();
}
}
class Tttt extends Thread{
private T t;
public Tttt(T t){
this.t = t;
}
public void run(){
this.t.c();
}
}
class Ttttt extends Thread{
private T t;
public Ttttt(T t){
this.t = t;
}
public void run(){
this.t.d();
}
}
T t = new T();
new Tttt(t).start();
new Tt(t).start();
new Ttt(t).start();
new Ttttt(t).start();
}
}
多个线程对象产生的多个线程,在执行同一对象的不同的同步的方法的时候,Thread.sleep(x),并不会释放相同对象的锁,而wait(x)会释放,这样可以不影响下面的线程执行其他同步的方法,在执行同一对象的不同的不同步的方法的时候,Thread.sleep(x),只阻塞当前线程,如果改成wait(x), 这本身就是有问题的,因为wait()只能在同步方法中调用。