我有一個迭代執行計算並每次更新一個類全局變量(函數運行迭代加深算法)的函數。我想找到一種方法做計算,然後返回5S給調用者後,全局變量的值,而無需等待計算完成:超時後的Java返回值
start computation
wait 5s
return global variable and terminate the computation function if not done
我想:
start computation in a new thread
curThread.sleep(5s)
return current global variable value and interrupt the computation thread
但線程終止有時甚至不能
感謝
你嘗試過什麼?你讀過什麼文件。你需要展示一些工作。這不是你的研究助攻。 – Gray