【发布时间】:2019-11-27 21:16:48
【问题描述】:
我的代码有问题。计时器总体上似乎工作正常,暂停按钮也能正常工作。
问题是当您在特定时间暂停时钟然后取消暂停时。
如果我们(比方说)在 8 秒暂停它并在一分钟后取消暂停,它不会像 9-10-11 那样继续运行,等等。它会转到 74-75-76...(我'我把它分成了几分钟和几秒钟)。
是不是线程导致了问题? (另外,我过度使用 freeze_sec 和 freeze_min 时间码 sn-ps 只是为了看看它是否会被修复,但事实并非如此。)
代码如下:
Thread t1 = null;
ss = new ServerSocket(6800);
while(true) {
s = ss.accept();
isr = new InputStreamReader(s.getInputStream());
br = new BufferedReader(isr);
message = br.readLine();
if (message.equals("START")) {
t1 = new Thread(new Thread1());
t1.start();
...
} else if (message.equals("PAUSE")) {
if(check) {
try {
check = false;
Thread1.PAUSE(true);
} catch (Exception e) {
System.out.println("Exception e");
}
} else {
check = true;
Thread1.PAUSE(false);
}
}
Thread1 类看起来像:
import java.io.*;
import java.util.Date;
import java.util.Scanner;
public class Thread1 extends MyServerFrame implements Runnable{
private static int current_min_time = 0;
private static int current_sec_time = 0;
private static int freeze_min_time = 0;
private static int freeze_sec_time = 0;
private static boolean pause = false;
private static int minutes = 0;
private int total_time_sec = 0;
private static boolean freeze_signal = false;
private static int k = 0;
@Override
public void run() {
long elapsedTime = 0L;
boolean bool = true;
int num = 0;
while (bool) {
Scanner scan = new Scanner(System.in);
if (minutes == 0) {
System.out.println("How many minutes for this half-time?");
Scanner in = new Scanner(System.in);
num = in.nextInt();
minutes = num;
}
long startTime = System.currentTimeMillis();
while (total_time_sec < minutes * 60 || freeze_signal == false) {
if (freeze_signal && k == 0) {
freeze_sec_time = current_sec_time;
freeze_min_time = current_min_time;
k++;
}
if (!pause) {
//perform db poll/check
if (elapsedTime / 1000 != current_sec_time) {
try {
clearTheFile("Half_Time.txt");
} catch (IOException e) {
System.out.println("Exception");
}
if (!freeze_signal && k > 0) {
current_sec_time = freeze_sec_time;
current_min_time = freeze_min_time;
k = 0;
}
current_sec_time++;
total_time_sec = current_sec_time + current_min_time / 60;
print_in_txt();
}
elapsedTime = (new Date()).getTime() - startTime;
if (current_sec_time == 60) {
if (!freeze_signal && k > 0) {
current_sec_time = freeze_sec_time;
current_min_time = freeze_min_time;
try {
clearTheFile("Half_Time.txt");
} catch (IOException e) {
System.out.println("Exception");
}
print_in_txt();
k = 0;
}
current_sec_time = 0;
current_min_time++;
total_time_sec = current_sec_time + current_min_time / 60;
startTime = System.currentTimeMillis();
elapsedTime = (new Date()).getTime() - startTime;
try {
clearTheFile("Half_Time.txt");
} catch (IOException e) {
System.out.println("Exception");
}
print_in_txt();
}
}
}
}
}
public static void clearTheFile(String txt_name) throws IOException {
try {
FileWriter fwOb = new FileWriter(txt_name, false);
PrintWriter pwOb = new PrintWriter(fwOb, false);
pwOb.flush();
pwOb.close();
fwOb.close();
} catch (IOException e) {}
}
public static void print_in_txt() {
PrintWriter out;
try {
out = new PrintWriter("Half_Time.txt");
out.println(String.format("%02d", current_min_time) + ":" + String.format("%02d", current_sec_time));
out.print("");
out.close();
} catch (FileNotFoundException e) {
System.err.println("File doesn't exist");
e.printStackTrace();
}
}
public static void PAUSE(boolean p) {
if (p) {
pause = true;
freeze_signal = true;
} else {
current_sec_time = freeze_sec_time;
current_min_time = freeze_min_time;
try {
clearTheFile("Half_Time.txt");
} catch (IOException e) {
System.out.println("Exception");
}
print_in_txt();
pause = false;
freeze_signal = false;
}
}
}
【问题讨论】:
-
你的代码有点乱,但是,你需要得到的基本概念是:你有一个“总运行时间”,这是你的时钟一直在运行的时间(不包括停顿)。然后你有一个“周期时间”,这是时钟在暂停之前允许运行的时间量。每次线程暂停时,您都会将“循环时间”添加到“总运行时间”并重置“循环时间”。
-
当时钟取消暂停时,您将获取当前时间并再次开始循环,其中“循环时间”成为该块自取消暂停以来一直运行的时间量
-
我也会避免使用
Date,而是使用java.time.Instant和java.time的持续时间支持 -
我今天试图在这个项目上挤出很多时间,但它是最后一个仍然被窃听的东西。这就是为什么它大多是一团糟。所以,如果我理解正确,我总是看到的是“总运行时间”,但我只是想删除暂停的持续时间。那是“循环时间”+“总时间”和“循环时间”的重置吗?如何重置“循环时间”?
-
好吧,我已经在很短的时间内对此束手无策,但后来我意识到,您实际上并不需要
Thread来实现计时。您只需要能够在检查时确定“当前”时间。为此,您可以使用一个简单的StopWatch类,for example
标签: java multithreading timer milliseconds