使一个线程停止有下列三种方式:
- 使用标记位(设置flag)停止线程;
- 调用stop方法强制停止线程;
- Thread类的interrupt方法;
一:使用flag停止线程
使用标记位
class Mythread3 implements Runnable
{
private boolean flag=true;
public void run()
{
int i=1;
while(flag)
{
try {
Thread.sleep(1000);
System.out.println(i+"次进入for循环" );
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
public class Stop1
{
public static void main(String[] args) throws InterruptedException {
Mythread3 thread=new Mythread3();
Thread thread1=new Thread(thread,"子线程");
thread1.start();
Thread.sleep(4000); //主线程4秒后会将thread1的falg置false,那么就会推出循环,子线程就会停止
thread.setFlag(false);
}
}
二:用stop使线程停止
使用stop
class Mythread3 implements Runnable
{
private boolean falg=true;
public void run()
{
System.out.print("子线程运行时间");
Stop1.printTime();
int i=1;
while(true)
{
try {
Thread.sleep(1000);
System.out.print("子线程第"+i+"次进入for循环时间");
Stop1.printTime();
i++;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Stop1
{
public static void main(String[] args) throws InterruptedException {
Mythread3 thread=new Mythread3();
Thread thread1=new Thread(thread,"子线程");
thread1.start();
System.out.println("");
System.out.println("");
System.out.print("子线程stop前4秒时间:");
printTime();
Thread.sleep(4000); //主线程4秒后会将执行thread1.stop,使子线程停止
thread1.stop(); //其实stop已经被过期声明
System.out.print("子线程结束时间:");
printTime();
}
public static void printTime()
{
Date date=new Date();
DateFormat dateFormat=new SimpleDateFormat(("yyyy-MM-dd HH:mm:ss"));
String time=dateFormat.format(date);
System.out.println(time);
}
}
为什么stop会被过期声明呢??
因为stop不安全:stop会解除由线程获取的所有锁定,当在一个线程对象上调用stop()方法时,这个线程对象所运行的线程就会立即停止,假如一个线程正在执行:synchronized void { x = 3; y = 4;} 由于方法是同步的,多个线程访问时总能保证x,y被同时赋值,而如果一个线程正在执行到x = 3;时,被调用了 stop()方法,即使在同步块中,它也会马上stop了,这样就产生了不完整的残废数据。
三.调用Thread类的interrupt方法
interrupt方法只是将线程状态置为中断而已,它不会中断一个正在运行的线程。此方法只是给线程传递一个中断信号,程序可以根据此信号判断是否要终止线程。
interrupt会抛异常:当线程中使用wait/sleep/join方法导致线程阻塞,则interrupt会在线程中抛一个InterruptException,并且将线程的中断状态由true置为false。
使用Thread.interupt方法
class Mythread3 implements Runnable
{
public void run() {
int i = 1;
while (true) {
try {
Thread.sleep(1000);
boolean bool = Thread.currentThread().isInterrupted();//当前线程是否被置为中断状态,是返回ture
//如果状态被置为中断状态,并且处于阻塞状态,会抛异常java.lang.InterruptedException,catch处理异常,不会走这个if语句
if (bool) {
System.out.println("非阻塞情况下执行该操作....线程状态" + bool);
break;
}
System.out.println("第" + i + "次执行,线程名称为:" + Thread.currentThread().getName());
i++;
} catch (InterruptedException e) {
//抛异常后,中断标志被系统会自动清除,线程中断状态由true变为false
boolean bool = Thread.currentThread().isInterrupted();
System.out.println(bool); //false
return; //退出run方法
}
}
}
}
public class Stop1
{
public static void main(String[] args) throws InterruptedException {
Mythread3 thread = new Mythread3();
Thread thread1 = new Thread(thread, "子线程");
thread1.start();
Thread.sleep(4000);
thread1.interrupt();//现在线程thread1为中断状态
}
}