如何从Java线程类的run方法返回值?

我编写了一个简单的Thread类,并从另一个类开始。我想从该Thread类的run()方法返回值。

我尝试过如下。当我逐行调试它时,它可以成功工作。但是当我运行它时,它不起作用。

public static void main(String[] args) {
    Threads treadClass = new Threads();
    int result = treadClass.threadMethod(10,5);
    System.out.println("Result : " + result);

}

类线程{

public int result;

public int threadMethod(int a,int b) {
    System.out.println("Inside threadMethod.");

    Thread thread = new Thread() {
        //Thread method
        public void run() {
            System.out.println("Inside run()");
            result = a + b;
        }
    };

    thread.start();
    return result;

}
vancehgame 回答:如何从Java线程类的run方法返回值?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3150939.html

大家都在问