无限死循环了

来源:2-5 Java线程-隋唐演义实战开发---关键先生

慕前端0336099

2016-03-30 18:17


public class wutai extends Thread {

public void run(){

CarryRunable zfj=new CarryRunable();

CarryRunable fpj=new CarryRunable();

//使用runnable创建线程

Thread zfzgj=new Thread(zfj,"政府正规军");

Thread fpzyj=new Thread(fpj,"反叛志愿军");

zfzgj.start();

fpzyj.start();

//线程的启动

try {

Thread.sleep(50);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("政府军表示我们有核弹定点打击了");

System.out.println("志愿军表示我们什么都怕就是不怕污染");

Thread dg=new KeySonThread();

dg.setName("良辰路过");

zfj.jundui=false;

fpj.jundui=false;

try {

Thread.sleep(2000);

} catch (InterruptedException e1) {

e1.printStackTrace();

}

dg.start();

}

try {

fpzyj.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("良辰有100种方法让你分分钟KO");

}


public static void main(String[] args) {

new wutai().start();

}

}

public class KeySonThread extends Thread {

public void run(){

System.out.println(Thread.currentThread().getName()+"上菜了");

for(int i=0;i<10;i++){

System.out.println(Thread.currentThread().getName()+"突然就咬死了"+i+"个");

}

System.out.println(Thread.currentThread().getName()+"歇菜");

}

}

public class CarryRunable implements Runnable {

volatile boolean jundui=true;

@Override

public void run() {

// TODO Auto-generated method stub

while(jundui){

for(int i=0;i<5;i++){

System.out.println(Thread.currentThread().getName()+"搞死了"+i+"个");

Thread.yield();

}

}

}


}


写回答 关注

2回答

  • _神人A
    2016-04-24 18:54:15
    已采纳
    package com.imooc.thread;
    
    public class wutai extends Thread {
    	public void run() {
    		CarryRunable zfj = new CarryRunable();
    		CarryRunable fpj = new CarryRunable();
    		// 使用runnable创建线程
    		Thread zfzgj = new Thread(zfj, "政府正规军");
    		Thread fpzyj = new Thread(fpj, "反叛志愿军");
    		zfzgj.start();
    		fpzyj.start();
    		// 线程的启动
    		try {
    			Thread.sleep(50);
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    			System.out.println("政府军表示我们有核弹定点打击了");
    			System.out.println("志愿军表示我们什么都怕就是不怕污染");
    			Thread dg = new KeySonThread();
    			dg.setName("良辰路过");
    			zfj.jundui = false;
    			fpj.jundui = false;
    			try {
    				Thread.sleep(2000);
    			} catch (InterruptedException e1) {
    				e1.printStackTrace();
    			}
    			dg.start();
    		try {
    			fpzyj.join();
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("良辰有100种方法让你分分钟KO");
    	}
    
    	public static void main(String[] args) {
    		new wutai().start();
    	}
    }

    你把wutai 这个class改成如上就ok了。你catch exception的括号写错了

    慕前端033...

    非常感谢!

    2016-04-25 13:26:10

    共 1 条回复 >

  • 原来我叫小土慕课网给我改了名字
    2016-03-30 23:32:40

    把CarryRunable和KeySonThread的代码也贴出来


    慕前端033...

    大神给帮看下撒

    2016-04-02 12:13:48

    共 1 条回复 >

深入浅出Java多线程

带你一起深入浅出多线程,掌握基础,展望进阶路线

186071 学习 · 498 问题

查看课程

相似问题