package com.jldx;
class StopThread extends Thread{
/**
* 停止线程标志
*/
public boolean flag = true;
@Override
public synchronized void run() {
while (flag){
try {
wait();
}catch (Exception e){
e.printStackTrace();
stopThread();
}
System.out.println(Thread.currentThread().getName() + "我是子线程");
}
}
/**
* 停止线程
*/
public void stopThread(){
flag = false;
System.out.println(getName() + "----线程被停止");
}
}
/**
* 中断线程
*/
public class ThreadDemo1 {
public static void main(String[] args) {
StopThread stopThread1 = new StopThread();
StopThread stopThread2 = new StopThread();
stopThread1.start();
stopThread2.start();
for (int i = 0; i <30; i++) {
try {
Thread.sleep(10);
}catch (Exception e){
}
System.out.println("main..." + i);
if(i == 29){
stopThread1.interrupt();
stopThread2.interrupt();
}
}
}
}
java的多线程中interrupt停止线程
最新推荐文章于 2024-08-30 18:40:43 发布