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));
}
}
} 作者: yaolv7 时间: 2016-5-13 17:12
用一个线程发红包,另外5个抢就可以了作者: pal_xie 时间: 2016-5-13 22:42
抢红包不是要设定总额和红包数量么,然后5条线程去抢,输出多少钱,而且抢的是一次,不是像楼主这样,5条线程各自抢各自的