gpt4 book ai didi

Java,停止(中断)线程

转载 作者:行者123 更新时间:2023-11-29 06:43:40 27 4
gpt4 key购买 nike

最近我问过这个问题,但无法解决这个问题。所以我有一个线程猎人(实际上有 2 个),他“去抓野 pig ”。他将这些公 pig 存放在容器冰箱中。他将继续这样做,直到他的工作时间结束。但是,如果冰箱已满,他就必须等待。目的是等到野 pig 从冰箱中取出,但如果时间超过 5 秒,则必须终止等待测试。所以除了一件事,一切都有效。运行测试并中断这些线程后,程序仍然继续运行。那么我该如何完全终止/停止这些线程呢?

测试类(主要)

class Test {

public static void main(String[] args) {
test1();
}

public static void test1() {

Fridge fridge = new Fridge(4);

Hunter hunter1 = new Hunter("hunter1", 4, fridge);
Hunter hunter2 = new Hunter("hunter2", 7, fridge);
Thread hunterThread1 = new Thread(hunter1);
Thread hunterThread2 = new Thread(hunter2);
hunterThread1.start();
hunterThread2.start();

try { Thread.sleep(1000); } catch (InterruptedException e) {}
hunterThread1.interrupt();
hunterThread2.interrupt();
System.out.println(fridge.getSize());
System.out.println(hunter1.getWorkTime());
System.out.println(hunter2.getWorkTime());
}
}

猎人级

class Hunter extends Worker {

private int workTime;
private Fridge fridge;

public Hunter(String name, int workTime, Fridge fridge) {
super(name);
this.workTime = workTime;
this.fridge = fridge;
}

public int getWorkTime() {
return workTime;
}

public void run() {
while (workTime > 0) {
/** Each hunt takes a random amount of time (1-50 ms) **/
try { Thread.sleep(workGen()); } catch (InterruptedException e) {}
/** Add new wild boars **/
try { fridge.add(new WildBoar()); } catch (InterruptedException e) {}
workTime--;
/** If thread is interupted break the loop **/
if( Thread.currentThread().isInterrupted()){
break;
}
}
}
}

冰箱类

import java.util.Stack;

class Fridge extends Storage {

private Stack<WildBoar> boars;

public Fridge(int cap) {
super(cap);
boars = new Stack<WildBoar>();
}

public int getCap() {
return cap;
}

public int getSize() {
return boars.size();
}

public boolean hasFreeSpace() {
if ( boars.size() < cap )
return true;
else
return false;
}

public synchronized void add(WildBoar boar) throws InterruptedException {
/** If there's no free space available wait **/
while ( !hasFreeSpace() ) {
wait();
}
/** Once there's free space available add new item **/
boars.add(boar);

}

public synchronized WildBoar remove() {
return boars.pop();
}

}

用于编译的其他类:

abstract class Worker implements Runnable {

private String name;

public Worker(String name) {
this.name = name;
}

public String getName() {
return name;
}

public int workGen() {
return 1 + (int)(Math.random() * (50 - 1));
}
}

class WildBoar {

public WildBoar() {}
}

abstract class Storage {

protected int cap;

public Storage(int cap) {
this.cap = cap;
}

public int getCap() {
return cap;
}
}

最佳答案

在您interrupt() 当前正在等待的线程之后, native 等待方法实际上会重置中断标志。因此,当您在此处评估 isInterrupted() 时,它实际上已重置并且将显示为未中断。

if( Thread.currentThread().isInterrupted()){
break;
}

waiting

期间发生中断后,您将不得不重新中断线程
public synchronized void add(Object boar) {
/** If there's no free space available wait **/
while (!hasFreeSpace()) {
try{
wait();
}catch(InterruptedException e){
Thread.currentThread().interrupt();
return; //or rethrow
}
}
/** Once there's free space available add new item **/
boars.add(boar);
}

关于Java,停止(中断)线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8581016/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com