class X{
int x;
}
class Y{
int y;
}
class A extends Thread{
X x;
Y y;
A(X x, Y y){
this.x = x;
this.y = y;
}
void func1(){
System.out.println("func1:start");
synchronized(x){
synchronized(y){
try{
sleep(10);
}catch(Exception e){}
}
}
}
void func2(){
System.out.println("func2:start");
synchronized(y){
synchronized(x){
try{
sleep(10);
}catch(Exception e){}
}
}
}
public void run(){
for( int i = 0 ; i < 1000 ; i++ ){
func1();
func2();
}
}
}
class Test{
public static void main(String args[]){
X x = new X();
Y y = new Y();
A a[] = new A[10];
for( int i = 0 ; i < 10 ; i++ ){
a[i] = new A(x, y);
a[i].start();
}
for( int i = 0 ; i < 10 ; i++ ){
try{
a[i].join();
}catch(Exception e){}
}
}
}
|