Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从另一个方法中断一个方法?_Java_Multithreading_Concurrency - Fatal编程技术网

Java 如何从另一个方法中断一个方法?

Java 如何从另一个方法中断一个方法?,java,multithreading,concurrency,Java,Multithreading,Concurrency,我有两种方法希望能够同时运行 public void bar(){ //Do some stuff which has a result //If result is true, then stop foo(). } public void foo() { for (int x = 0; x <= 100; ++x) { //Do some stuff... //Execute bar, but don't wait for

我有两种方法希望能够同时运行

public void bar(){
    //Do some stuff which has a result
    //If result is true, then stop foo().
}

public void foo()
{
    for (int x = 0; x <= 100; ++x)
    {
        //Do some stuff...
        //Execute bar, but don't wait for it to finish before jumping to next iteration.
    }
}
公共空白栏(){
//做一些有结果的事情
//如果结果为true,则停止foo()。
}
公共图书馆
{

对于(intx=0;x我不太确定你到底想做什么。 但听起来你的问题可以通过排队来解决

queue = Queue(max_size=n)

thread_1:
    while True:
        x = foo()
        queue.put(x)

thread_2:
    while True:
        x = queue.get()
        doSomething(x)
但是,我不想每次都创建一个新线程来执行bar()的新迭代

您可以实现它,它将为您处理线程,这样您的程序就不会为B任务不断生成新线程——它将获取工作线程。值得考虑

顺便说一句:应该给你一个关于如何管理线程中断的提示

编辑:你可以

请记住,这是一个非常花哨的解决方案,我建议你重新考虑一下你的并发架构。这篇文章中有很多很好的解决方案——试试看

void foo() {
    Thread fooThread = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                try {
                    bar(); // Spawns barThread, but continues execution
                    Thread.sleep(2000); // foo has to return after bar, else NullPointerException is thrown
                } catch (InterruptedException e) {
                    //handle interruption
                }
            }
        }
    }, "fooThread"); // here im assigning name to fooThread
    fooThread.start();
}

void bar() {
    Thread barThread = new Thread(new Runnable() {
        @Override
        public void run() {
            // do something that interrupts foo()
            getThreadByName("fooThread").interrupt();
        }
    });

    barThread.start();
}
void foo(){
Thread fooThread=新线程(new Runnable(){
@凌驾
公开募捐{
对于(int i=0;i<5;i++){
试一试{
bar();//生成barThread,但继续执行
Thread.sleep(2000);//foo必须在bar之后返回,否则将抛出NullPointerException
}捕捉(中断异常e){
//处理中断
}
}
}
},“fooThread”);//这里我给fooThread分配名称
fooThread.start();
}
空条(){
Thread barThread=新线程(new Runnable(){
@凌驾
公开募捐{
//做一些打断foo()的事情
getThreadByName(“fooThread”).interrupt();
}
});
barThread.start();
}

创建新线程。这就是线程的用途。需要详细说明吗?如果您担心创建多个线程,请创建一个线程池大小有限的ExecutorService我知道我可以使用标志来检查是否停止执行,但要获得所需的功能,我必须在每行代码之后检查标志h是不实用的。我希望有点像一个ActionListener,当bar()告诉foo()停止时,它会监听。这是(pythonlike)伪代码。我只想解释这个概念,而不是具体的实现
void foo() {
    Thread fooThread = new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                try {
                    bar(); // Spawns barThread, but continues execution
                    Thread.sleep(2000); // foo has to return after bar, else NullPointerException is thrown
                } catch (InterruptedException e) {
                    //handle interruption
                }
            }
        }
    }, "fooThread"); // here im assigning name to fooThread
    fooThread.start();
}

void bar() {
    Thread barThread = new Thread(new Runnable() {
        @Override
        public void run() {
            // do something that interrupts foo()
            getThreadByName("fooThread").interrupt();
        }
    });

    barThread.start();
}