我編寫了一個非常簡單的玩具程序,根據用戶指定的時間要求完成線程內任務的執行。代碼和示例輸出如下。會發生什麼情況是,每次運行代碼時,任務完成時間都會在用戶指定的時間的+ delta範圍內。例如,如果用戶想要在5秒內完成程序,則根據運行代碼的CPU,它可以在5093或5012毫秒內完成。我想添加一些代碼,它可以自動確定特定CPU爲特定版本的JVM提供的最低延遲。根據該檢測代碼,可以將一個delta值添加到該行中,如:if ((taskRunTime > patience+delta) && t.isAlive())
,以便系統爲任務執行時間帶來更高精度。請提出一些建議。確定Java程序的確定性線程執行
代碼:
public class ThreadExample
{
static void threadMessage(String message)
{
String threadName = Thread.currentThread().getName();
System.out.format("%s: %s%n", threadName, message);
}
private static class MessageLoop implements Runnable
{
public void run()
{
String importantInfo[] =
{
"A new integrated approach to programming",
"The innovative approach of the system",
"The input of a tracking system",
"A simulation system is then used for collision checking"
};
try
{
for (int i = 0; i < importantInfo.length; i++)
{
Thread.sleep(4000);
threadMessage(importantInfo[i]);
}
}
catch (InterruptedException e)
{
threadMessage("I wasn't done!");
}
}
}
public static void main(String args[]) throws InterruptedException
{
//Delay, in milliseconds before we interrupt MessageLoop
long patience = 1000 * 60 * 60;
//If command line argument present, gives patience in seconds.
if (args.length > 0)
{
try {
patience = Long.parseLong(args[0]) * 1000;
} catch (NumberFormatException e) {
System.err.println("Argument must be an integer.");
System.exit(1);
}
}
threadMessage("Starting MessageLoop thread");
long startTime = System.currentTimeMillis(),taskRunTime=0;
Thread t = new Thread(new MessageLoop());
t.start();
threadMessage("Waiting for MessageLoop thread to finish");
//loop until MessageLoop thread exits
while (t.isAlive())
{
threadMessage("Still waiting...");
//Wait maximum of 1 second for MessageLoop thread to finish.
t.join(100);
taskRunTime=System.currentTimeMillis() - startTime;
if ((taskRunTime > patience) && t.isAlive())
{
threadMessage("Tired of waiting...task is running longer than the patience you set or the default!");
t.interrupt();
t.join();
}
}
threadMessage("Finally out of thread!");
System.out.println("Time to complete task="+taskRunTime+"ms");
}
}
從英特爾迅馳1.7 Ghz的計算機輸出樣品(Java的熱點(TM)客戶端虛擬機(建設10.0 - B23,混合模式))
java -jar ThreadExample.jar 5
main: Starting MessageLoop thread
main: Waiting for MessageLoop thread to finish
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
Thread-0: A new integrated approach to programming
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Still waiting...
main: Tired of waiting...task is running longer than the patience you set or the default!
Thread-0: I wasn't done!
main: Finally out of thread!
那麼你建議使用併發工具?我在哪裏可以找到允許確定性執行併發任務的代碼編寫示例?在這個特別簡單的例子中,我並沒有對任務協調和併發性感到困擾,因爲我正在確定決定論。當然,一旦我安排了多項任務,問題就會出現,但我想先從簡單的事情開始。 – iceman 2009-08-11 22:15:32