标签:
线程停止
方式1 stop(不推荐)
package com.exmaple.Thread;
public class Test1 {
/**
* 线程停止 方式1 stop(不推荐)
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Runna r=new Runna();
Thread t=new Thread(r);
t.start();
try{
Thread.sleep(5000);
}catch(InterruptedException e){
return ;
}
t.stop();
}
}
class Runna implements Runnable{
public static boolean flag=true;
@Override
public void run() {
// TODO Auto-generated method stub
while(flag){
System.out.println("hello");
}
}
}
* 方式2: 通过改变线程的运行状态
package com.exmaple.Thread;
public class Test1 {
/**
* 方式2: 通过改变线程的运行状态
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Runna r=new Runna();
Thread t=new Thread(r);
t.start();
try{
Thread.sleep(5000);
}catch(InterruptedException e){
return ;
}
// t.stop();
r.flag=false;
}
}
class Runna implements Runnable{
public static boolean flag=true;
@Override
public void run() {
// TODO Auto-generated method stub
while(flag){
System.out.println("hello");
}
}
}
标签:
原文地址:http://www.cnblogs.com/meijing/p/4580465.html