优化了,应该算是完成了,用的Runnable接口
[Java] 纯文本查看 复制代码 package com.lc.test20171007;
import java.util.Random;
public class test06 {
public static void main(String[] args) {
Threads threads = new Threads();
new Thread(threads, "A").start();
new Thread(threads, "B").start();
new Thread(threads, "C").start();
new Thread(threads, "D").start();
new Thread(threads, "E").start();
}
}
//实现Runnable接口
class Threads implements Runnable {
private int count = 3;// 红包个数
@Override
public void run() {
Random random = new Random();
//线程进来一次红包个数-1
if (count-- > 0) {
System.out.println("恭喜" + Thread.currentThread().getName()
+ "用户抢到了" + (random.nextInt(10) + 1) + "块钱!");
}
Thread.yield();// 暂停当前正在执行的线程对象,把执行机会让给相同或者更高优先级的线程
}
}
|