ourInterrupt() 方法必须中断 threads 列表中的所有线程。 要怎麼一次中斷全部
run() 方法必须在当前线程上调用 isInterrupted() 方法。 我想調用但沒辦法調用
package zh.codegym.task.task16.task1620;
import com.sun.org.apache.xpath.internal.objects.XBoolean;
import java.util.ArrayList;
import java.util.List;
/*
一生万物,万物归一
*/
public class Solution {
public static boolean flag = false;
public static byte threadCount = 3;
static List<Thread> threads = new ArrayList<>(threadCount);
public static void main(String[] args) throws InterruptedException {
initThreadsAndStart();
Thread.sleep(3000);
ourInterrupt();
}
public static void ourInterrupt() {
Thread.interrupted();
}
private static void initThreadsAndStart() {
Water water = new Water("水");
for (int i = 0; i < threadCount; i++) {
threads.add(new Thread(water, "#" + i));
}
for (int i = 0; i < threadCount; i++) {
threads.get(i).start();
}
}
public static class Water implements Runnable {
private String sharedResource;
public Water(String sharedResource) {
this.sharedResource = sharedResource;
}
public void run() {
//修复 2 个变量
boolean isCurrentThreadInterrupted = false;
String threadName = Thread.currentThread().getName();
try {
while (!isCurrentThreadInterrupted) {
System.out.println("对象 " + sharedResource + ",线程 " + threadName);
Thread.sleep(1000);
}
} catch (InterruptedException ignored) {
}
}
}
}