public static void main(String[] args) {
// TODO Auto-generated method stub
final Qiang q = new Qiang();
new Thread(){public void run() {
while(true){
try {
q.bag1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
new Thread(){public void run() {
while(true){
try {
q.bag2();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
new Thread(){public void run() {
while(true){
try {
q.bag3();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
new Thread(){public void run() {
while(true){
try {
q.bag4();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
new Thread(){public void run() {
while(true){
try {
q.bag5();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();
}
}
class Lock{}
class Qiang{
Lock lo = new Lock();
public void bag1() throws Exception{
Random r = new Random();
synchronized(lo){
Thread.sleep(300);
System.out.println("第一个"+"="+(r.nextInt(10)+1));
}
}
public void bag2() throws Exception{
Random r = new Random();
synchronized(lo){
Thread.sleep(300);
System.out.println("第二个"+"="+(r.nextInt(10)+1));
}
}
public void bag3() throws Exception{
Random r = new Random();
synchronized(lo){
Thread.sleep(300);
System.out.println("第三个"+"="+(r.nextInt(10)+1));
}
}
public void bag4() throws InterruptedException{
Random r = new Random();
synchronized(lo){
Thread.sleep(300);
System.out.println("第四个"+"="+(r.nextInt(10)+1));
}
}
public void bag5() throws Exception{
Random r = new Random();
synchronized(lo){
Thread.sleep(300);
System.out.println("第五个"+"="+(r.nextInt(10)+1));
}
}
}