本帖最后由 郝福明 于 2012-12-29 20:45 编辑
当我继承Thread的时候,cpu没事,而且程序会结束,但是用实现Runnable的时候,程序就会一直运行,而且cpu占到100%- public class salePiao {
- public static void main(String[] args) {
- Ticket t = new Ticket();
-
- Thread t1 = new Thread(t);
- Thread t2 = new Thread(t);
- Thread t3 = new Thread(t);
- Thread t4 = new Thread(t);
-
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
- class Ticket implements Runnable{ //extends Thread{
- private int tick = 10;
- public void run(){
- while(true){
- if(tick>0){
- System.out.println(Thread.currentThread().getName()+"sale:"+tick--);
- }
- }
- }
- }
复制代码 |