我偶然发现了一个我不太清楚的问题。
A. Using Java 1.4, wrap the following function in a thread so that it can be called asynchronously and provide a way for the return value to be retrieved at a later time:
B. How would the same be done in Java 5
int someMethod(int i) { return i++; }
我认为解决方案之一是:编写一个具有两个公共(public)方法 void Synchronizedcalculate(int i)
和 int getValue()
的类。 calculate
启动线程并设置一个私有(private)变量。
在java 1.5中我可以使用AtomincInteger。这是一个答案吗?
最佳答案
在 Java 1.5 中,我很确定您会使用 Future
返回结果。我不确定 1.4 的等效项,但它看起来像 this question覆盖相同的地面。
关于java - 带线程的异步函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14285973/