嗨,我在我的大学有一个小组项目。我和我的小组成员需要创建一个网站。 我们已经这样做了,今年我们学习了Java编程语言。我的讲师给了我们另一项任务,即使用Java将我们的网站从静态升级到动态,特别是使用JSP(JavaServer Pages)。
我有责任为我们的网站创建这项新任务的统计报告。到目前为止,我能够找到如何从互联网计算平均值,总和和中位数,特别是从这个论坛。但我无法找到如何计算管理员根据String ArrayList回答用户提出的问题所花费的时间。
有人可以帮我这个吗?我真的很喜欢这个Java,任何帮助都会受到赞赏。
我很抱歉我的语法,英语不是我的主要语言。提前谢谢!
答案 0 :(得分:1)
使用线程来保持时间:
public class KeepingTime
{
private volatile int timeTaken;
private Thread timekeeper;
public KeepingTime()
{
timekeeper = new Thread(new Runnable(){
public void run()
{
try
{
while(true)
{
Thread.sleep(1000);
timeTaken++;
if(Thread.interrupted())
return;
}
}
catch(InterruptedException e)
{
return;
}
} //end run
}); //end thread
timekeeper.start();
} //end constructor
public void stop()
{
timekeeper.interrupt();
}
public int timeTaken()
{
return timeTaken;
}
}
然后像这样使用它:
for(String question : questions)
{
KeepingTime timer = new KeepingTime();
//ask the question
//wait until it is answered
timer.stop();
int timeTaken = timer.timeTaken();
}
请注意,我不使用Thread.stop(),因为它已被弃用。如果您运行大量线程,这可能会不一致。