慕前端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();
}
}
}
}
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的括号写错了
把CarryRunable和KeySonThread的代码也贴出来
深入浅出Java多线程
186071 学习 · 498 问题
相似问题