我编写了一个非常简单的玩具程序,按照用户指定的时序要求,在线程内完成任务的执行。代码和示例输出如下。发生的情况是,每次运行代码时,任务完成时间都会在用户指定时间的 + delta 范围内。例如,如果用户想在 5 秒内完成程序,它可能会在 5093 或 5012 毫秒内完成,具体取决于代码运行所在的 CPU。我想添加一些代码,这些代码可以自动确定特定 CPU 将为特定版本的 JVM 提供的最低延迟。基于该检测代码,可以将 delta 值添加到行中,例如 :if (( taskRunTime > patience+delta) && t.isAlive())
以便系统为任务执行时间带来更精确的结果。请给一些建议。
代码:
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");
}
}
来自 Intel Centrino 1.7 Ghz 机器的示例输出(Java HotSpot(TM) Client VM(build 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!