异步返回延迟 - IllegalMonitorStateException

时间:2018-04-16 13:16:39

标签: java multithreading asynchronous callable

我试图在新线程中调用方法,它将在延迟后使用callable返回一些东西。它由于 IllegalMonitorStateException 而下降 是否有可能以这种方式封装线程服务,当我只创建类的实例时,调用方法并且该方法将返回具有延迟的对象? 提前谢谢。

// ENTITY
public class Result {
    public String name;
    public int value;

    public Result(String name, int value) {
        this.name = name;
        this.value = value;
    }

    public Result() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Result{" +
                "name='" + name + '\'' +
                ", value=" + value +
                '}';
    }
}

创建具有延迟和返回值的线程

import java.util.concurrent.*;

public class AsyncCallable {

    public Result calculate() throws ExecutionException, InterruptedException {

        int delay = 5000;
        ExecutorService executorService = Executors.newSingleThreadExecutor();

        Callable<Result> callable = () -> {
            this.wait(delay);
            return new Result("Name", 4);
        };

        Future<Result> future = executorService.submit(callable);
        executorService.shutdown();
        return future.get();
    }

}

通话方式:

import java.util.concurrent.ExecutionException;

public class Main {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        AsyncCallable asyncCallable = new AsyncCallable();

        System.out.println(asyncCallable.calculate().toString());


    }
}

2 个答案:

答案 0 :(得分:1)

由于您的ExecutorService仅使用一个帖子,因此只需拨打Thread#sleep而不是Object#wait即可:

Callable<Result> callable = () -> {
    Thread.sleep(5_000);
    return new Result("Name", 4);
};

答案 1 :(得分:1)

好吧,为了坚持你的情况,任何对wait()的调用都必须包含在synchronized块中(参见方法javadoc here)。但是,我认为您应该考虑使用内置工具,例如Scheduled thread pool。使用此功能,您可以执行以下操作:

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.Executors;

final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(4);
// Execute a task after 5 seconds
scheduler.schedule(() -> new Result("Name", 4), 5, TimeUnit.SECONDS);