Java保存线程结果在一个变量从主类



我通过实现Runnable接口创建了一个线程。我的任务是这样的:线程应该计数(从0开始)并保存它已经达到的值,这是主类的一部分。我覆盖了运行方法,并做了计数,但我如何将这个值保存到主变量?只有getter才是解决方案?我的线程类:

public class myThread implements Runnable {
private static int count = 0;
private final int length;
public myThread (int length) {
this.length = length;
}
@Override
public void run() {
while (count < this.length) {
increaseValue();
}
}
private void increaseValue() {
synchronized (ThreadFirstExercise.class) {
if (count < this.length) {
this.count++;
}
}
}
}

主:

public class Main {
public static void main(String[] args) {
int result; // this is the variable where I want to save the counting result of my thread
(new Thread(new ThreadFirstExercise(10000))).start();
}
}

您可以使用返回FutureCallable。或者您可以在main方法中使用包含整数的包装器对象。