Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的计时器在java中以随机间隔停止 导入java.awt.event.ActionEvent; 导入java.awt.event.ActionListener; 导入javax.swing.Timer; 导入org.eclipse.swt.widgets.Display; 导入org.eclipse.swt.widgets.Shell; 导入org.eclipse.swt.custom.CLabel; 导入org.eclipse.swt.swt; 公共级吉他测试3{ //声明GUI的各个部分 私有静态Shell=新Shell(); CLabel SENSOR TITLELABEL=新CLabel(外壳,SWT.SHOW_OUT); CLabel sensorNum_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel lblBattery=新CLabel(外壳,SWT.SHADOW_OUT); CLabel lblLastAlert=新CLabel(外壳,SWT.SHADOW_OUT); CLabel lblAlert=新CLabel(外壳,SWT.SHADOW_OUT); CLabel batter_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); 最终CLabel lstAlert_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel alert_1=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); CLabel sensorNum_2=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel battery_2=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); CLabel lstAlert_2=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel alert_2=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); 公共吉他3() { //默认构造函数为GUI设置值 外壳尺寸(450300); shell.setText(“SWT应用程序”); 传感器标题标签设置对齐(SWT中心); 传感器标题标签立根(10,10,100,21); sensorTitleLabel.setText(“传感器编号”); sensorNum_1.立根(10,37,100,21); sensorNum_1.setText(“”); lblBattery.setAlignment(SWT.CENTER); lblBattery.setText(“电池”); lblBattery.立根(116,10,100,21); LBLLASTALRT设置对齐(SWT中心); lblLastAlert.setText(“上次警报”); lblLastAlert.立根(222,10100,21); lblAlert.设置校准(SWT.中心); lblAlert.setText(“警报”); lblAlert.立根(328,10100,21); 电池1.setText(“”); 击球手1.后退(116、37、100、21); lstart_1.setText(“”); 1.挫折(222、37、100、21); 警报1.setText(“”); 警报1.倒退(328、37、100、21); sensorNum_2.setText(“”); sensorNum_2.立根(10,64,100,21); 电池组2.setText(“”); 电池组2.立根(116、64、100、21); lstart_2.setText(“”); lstarter_2.立根(222,64,100,21); 警报2.setText(“”); 警报2.倒退(328、64、100、21); 已同步(此) { //启动计时器,传入GUI 新建AlertTimer(this.start(); } } /** *启动应用程序。 *@param args */ 公共静态void main(字符串[]args){ 试一试{ GUITest3窗口=新的GUITest3(); window.open(); }捕获(例外e){ e、 printStackTrace(); } } /** *打开窗户。 */ 公开作废{ Display=Display.getDefault(); shell.open(); shell.layout(); 而(!shell.isDisposed()){ 如果(!display.readAndDispatch()){ display.sleep(); } } } 公共void setTimerLabel(最终字符串timeLeft) { //用于更改显示的时间 Display.getDefault().asyncExec(新的Runnable()){ 公开募捐 { lstAlert_1.setText(timeLeft); } }); } } 类警报计时器{ 私有静态final int TIMER_PERIOD=1000;//以1秒的间隔发出声音 受保护静态最终整数MAX_COUNT=300;//最大计数,即五分钟 私人吉他3图形用户界面; 私有整数计数=0; 公共警报计时器(GUITest3屏幕){ gui=屏幕; 字符串文本=(计数/60)+“:0”+(计数%60); screenie.setTimerLabel(文本); } 公开作废开始(){ 新计时器(计时器周期,新ActionListener(){ @凌驾 已执行的公共同步作废操作(ActionEvent e){ 如果(计数9) { text=(计数/60)+“:”+(计数%60); } 其他的 text=(计数/60)+“:0”+(计数%60); System.out.println(文本); gui.setTimerLabel(文本); }否则{ //((计时器)e.getSource()).stop(); Display.getDefault().syncExec(新的Runnable()){ 公开募捐 { 同步(gui) { AlertFlash alrt1=新的AlertFlash(gui); 螺纹闪烁=新螺纹(alrt1); flash.start(); } } }); } } }).start(); } }_Java_Multithreading_Timer - Fatal编程技术网

我的计时器在java中以随机间隔停止 导入java.awt.event.ActionEvent; 导入java.awt.event.ActionListener; 导入javax.swing.Timer; 导入org.eclipse.swt.widgets.Display; 导入org.eclipse.swt.widgets.Shell; 导入org.eclipse.swt.custom.CLabel; 导入org.eclipse.swt.swt; 公共级吉他测试3{ //声明GUI的各个部分 私有静态Shell=新Shell(); CLabel SENSOR TITLELABEL=新CLabel(外壳,SWT.SHOW_OUT); CLabel sensorNum_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel lblBattery=新CLabel(外壳,SWT.SHADOW_OUT); CLabel lblLastAlert=新CLabel(外壳,SWT.SHADOW_OUT); CLabel lblAlert=新CLabel(外壳,SWT.SHADOW_OUT); CLabel batter_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); 最终CLabel lstAlert_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel alert_1=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); CLabel sensorNum_2=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel battery_2=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); CLabel lstAlert_2=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel alert_2=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); 公共吉他3() { //默认构造函数为GUI设置值 外壳尺寸(450300); shell.setText(“SWT应用程序”); 传感器标题标签设置对齐(SWT中心); 传感器标题标签立根(10,10,100,21); sensorTitleLabel.setText(“传感器编号”); sensorNum_1.立根(10,37,100,21); sensorNum_1.setText(“”); lblBattery.setAlignment(SWT.CENTER); lblBattery.setText(“电池”); lblBattery.立根(116,10,100,21); LBLLASTALRT设置对齐(SWT中心); lblLastAlert.setText(“上次警报”); lblLastAlert.立根(222,10100,21); lblAlert.设置校准(SWT.中心); lblAlert.setText(“警报”); lblAlert.立根(328,10100,21); 电池1.setText(“”); 击球手1.后退(116、37、100、21); lstart_1.setText(“”); 1.挫折(222、37、100、21); 警报1.setText(“”); 警报1.倒退(328、37、100、21); sensorNum_2.setText(“”); sensorNum_2.立根(10,64,100,21); 电池组2.setText(“”); 电池组2.立根(116、64、100、21); lstart_2.setText(“”); lstarter_2.立根(222,64,100,21); 警报2.setText(“”); 警报2.倒退(328、64、100、21); 已同步(此) { //启动计时器,传入GUI 新建AlertTimer(this.start(); } } /** *启动应用程序。 *@param args */ 公共静态void main(字符串[]args){ 试一试{ GUITest3窗口=新的GUITest3(); window.open(); }捕获(例外e){ e、 printStackTrace(); } } /** *打开窗户。 */ 公开作废{ Display=Display.getDefault(); shell.open(); shell.layout(); 而(!shell.isDisposed()){ 如果(!display.readAndDispatch()){ display.sleep(); } } } 公共void setTimerLabel(最终字符串timeLeft) { //用于更改显示的时间 Display.getDefault().asyncExec(新的Runnable()){ 公开募捐 { lstAlert_1.setText(timeLeft); } }); } } 类警报计时器{ 私有静态final int TIMER_PERIOD=1000;//以1秒的间隔发出声音 受保护静态最终整数MAX_COUNT=300;//最大计数,即五分钟 私人吉他3图形用户界面; 私有整数计数=0; 公共警报计时器(GUITest3屏幕){ gui=屏幕; 字符串文本=(计数/60)+“:0”+(计数%60); screenie.setTimerLabel(文本); } 公开作废开始(){ 新计时器(计时器周期,新ActionListener(){ @凌驾 已执行的公共同步作废操作(ActionEvent e){ 如果(计数9) { text=(计数/60)+“:”+(计数%60); } 其他的 text=(计数/60)+“:0”+(计数%60); System.out.println(文本); gui.setTimerLabel(文本); }否则{ //((计时器)e.getSource()).stop(); Display.getDefault().syncExec(新的Runnable()){ 公开募捐 { 同步(gui) { AlertFlash alrt1=新的AlertFlash(gui); 螺纹闪烁=新螺纹(alrt1); flash.start(); } } }); } } }).start(); } }

我的计时器在java中以随机间隔停止 导入java.awt.event.ActionEvent; 导入java.awt.event.ActionListener; 导入javax.swing.Timer; 导入org.eclipse.swt.widgets.Display; 导入org.eclipse.swt.widgets.Shell; 导入org.eclipse.swt.custom.CLabel; 导入org.eclipse.swt.swt; 公共级吉他测试3{ //声明GUI的各个部分 私有静态Shell=新Shell(); CLabel SENSOR TITLELABEL=新CLabel(外壳,SWT.SHOW_OUT); CLabel sensorNum_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel lblBattery=新CLabel(外壳,SWT.SHADOW_OUT); CLabel lblLastAlert=新CLabel(外壳,SWT.SHADOW_OUT); CLabel lblAlert=新CLabel(外壳,SWT.SHADOW_OUT); CLabel batter_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); 最终CLabel lstAlert_1=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel alert_1=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); CLabel sensorNum_2=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel battery_2=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); CLabel lstAlert_2=新CLabel(外壳,SWT.BORDER | SWT.SHADOW_IN); CLabel alert_2=新CLabel(外壳,SWT.BORDER | SWT.SHOW_IN); 公共吉他3() { //默认构造函数为GUI设置值 外壳尺寸(450300); shell.setText(“SWT应用程序”); 传感器标题标签设置对齐(SWT中心); 传感器标题标签立根(10,10,100,21); sensorTitleLabel.setText(“传感器编号”); sensorNum_1.立根(10,37,100,21); sensorNum_1.setText(“”); lblBattery.setAlignment(SWT.CENTER); lblBattery.setText(“电池”); lblBattery.立根(116,10,100,21); LBLLASTALRT设置对齐(SWT中心); lblLastAlert.setText(“上次警报”); lblLastAlert.立根(222,10100,21); lblAlert.设置校准(SWT.中心); lblAlert.setText(“警报”); lblAlert.立根(328,10100,21); 电池1.setText(“”); 击球手1.后退(116、37、100、21); lstart_1.setText(“”); 1.挫折(222、37、100、21); 警报1.setText(“”); 警报1.倒退(328、37、100、21); sensorNum_2.setText(“”); sensorNum_2.立根(10,64,100,21); 电池组2.setText(“”); 电池组2.立根(116、64、100、21); lstart_2.setText(“”); lstarter_2.立根(222,64,100,21); 警报2.setText(“”); 警报2.倒退(328、64、100、21); 已同步(此) { //启动计时器,传入GUI 新建AlertTimer(this.start(); } } /** *启动应用程序。 *@param args */ 公共静态void main(字符串[]args){ 试一试{ GUITest3窗口=新的GUITest3(); window.open(); }捕获(例外e){ e、 printStackTrace(); } } /** *打开窗户。 */ 公开作废{ Display=Display.getDefault(); shell.open(); shell.layout(); 而(!shell.isDisposed()){ 如果(!display.readAndDispatch()){ display.sleep(); } } } 公共void setTimerLabel(最终字符串timeLeft) { //用于更改显示的时间 Display.getDefault().asyncExec(新的Runnable()){ 公开募捐 { lstAlert_1.setText(timeLeft); } }); } } 类警报计时器{ 私有静态final int TIMER_PERIOD=1000;//以1秒的间隔发出声音 受保护静态最终整数MAX_COUNT=300;//最大计数,即五分钟 私人吉他3图形用户界面; 私有整数计数=0; 公共警报计时器(GUITest3屏幕){ gui=屏幕; 字符串文本=(计数/60)+“:0”+(计数%60); screenie.setTimerLabel(文本); } 公开作废开始(){ 新计时器(计时器周期,新ActionListener(){ @凌驾 已执行的公共同步作废操作(ActionEvent e){ 如果(计数9) { text=(计数/60)+“:”+(计数%60); } 其他的 text=(计数/60)+“:0”+(计数%60); System.out.println(文本); gui.setTimerLabel(文本); }否则{ //((计时器)e.getSource()).stop(); Display.getDefault().syncExec(新的Runnable()){ 公开募捐 { 同步(gui) { AlertFlash alrt1=新的AlertFlash(gui); 螺纹闪烁=新螺纹(alrt1); flash.start(); } } }); } } }).start(); } },java,multithreading,timer,Java,Multithreading,Timer,所以,这是一个gui,它显示一个计时器并开始计数,当它达到5:00时停止(那时做了一些事情,但仍在工作,我知道它有问题,但我会在考虑这个计时器时继续使用它)。 由于某些原因,我不明白,计时器偶尔会停止。我已经让计时器在4:18、0:02、2:13等时间停止计数,我花了一些时间来观察它是否有规律,看看这是否会让我得出任何结论,但我看不到规律。也没有错误消息,它只是停止 我认为这与线程方面有关(我承认,我的知识非常缺乏,仍然在那里),可能是缺少了“Synchronized”或“volatile”或类

所以,这是一个gui,它显示一个计时器并开始计数,当它达到5:00时停止(那时做了一些事情,但仍在工作,我知道它有问题,但我会在考虑这个计时器时继续使用它)。 由于某些原因,我不明白,计时器偶尔会停止。我已经让计时器在4:18、0:02、2:13等时间停止计数,我花了一些时间来观察它是否有规律,看看这是否会让我得出任何结论,但我看不到规律。也没有错误消息,它只是停止

我认为这与线程方面有关(我承认,我的知识非常缺乏,仍然在那里),可能是缺少了“Synchronized”或“volatile”或类似的,我已经尝试添加和删除了奇数
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.SWT;


public class GUITest3 {


    //Declare sections of GUI
    private static Shell shell = new Shell();
    CLabel sensorTitleLabel = new CLabel(shell, SWT.SHADOW_OUT);
    CLabel sensorNum_1 = new CLabel(shell, SWT.BORDER | SWT.SHADOW_IN);
    CLabel lblBattery = new CLabel(shell, SWT.SHADOW_OUT);
    CLabel lblLastAlert = new CLabel(shell, SWT.SHADOW_OUT);
    CLabel lblAlert = new CLabel(shell, SWT.SHADOW_OUT);
    CLabel batter_1 = new CLabel(shell, SWT.BORDER | SWT.SHADOW_IN);
    final CLabel lstAlert_1 = new CLabel(shell, SWT.BORDER | SWT.SHADOW_IN);
    CLabel alert_1 = new CLabel(shell, SWT.BORDER | SWT.SHADOW_IN);
    CLabel sensorNum_2 = new CLabel(shell, SWT.BORDER | SWT.SHADOW_IN);
    CLabel battery_2 = new CLabel(shell, SWT.BORDER | SWT.SHADOW_IN);
    CLabel lstAlert_2 = new CLabel(shell, SWT.BORDER | SWT.SHADOW_IN);
    CLabel alert_2 = new CLabel(shell, SWT.BORDER | SWT.SHADOW_IN);


    public GUITest3()
    {
        //Default constructor sets up the values for the GUI
        shell.setSize(450, 300);
        shell.setText("SWT Application");

        sensorTitleLabel.setAlignment(SWT.CENTER);
        sensorTitleLabel.setBounds(10, 10, 100, 21);
        sensorTitleLabel.setText("Sensor Number");

        sensorNum_1.setBounds(10, 37, 100, 21);
        sensorNum_1.setText("");

        lblBattery.setAlignment(SWT.CENTER);
        lblBattery.setText("Battery");
        lblBattery.setBounds(116, 10, 100, 21);

        lblLastAlert.setAlignment(SWT.CENTER);
        lblLastAlert.setText("Last Alert");
        lblLastAlert.setBounds(222, 10, 100, 21);

        lblAlert.setAlignment(SWT.CENTER);
        lblAlert.setText("Alert");
        lblAlert.setBounds(328, 10, 100, 21);

        batter_1.setText("");
        batter_1.setBounds(116, 37, 100, 21);

        lstAlert_1.setText("");
        lstAlert_1.setBounds(222, 37, 100, 21);

        alert_1.setText("");
        alert_1.setBounds(328, 37, 100, 21);

        sensorNum_2.setText("");
        sensorNum_2.setBounds(10, 64, 100, 21);

        battery_2.setText("");
        battery_2.setBounds(116, 64, 100, 21);

        lstAlert_2.setText("");
        lstAlert_2.setBounds(222, 64, 100, 21);

        alert_2.setText("");
        alert_2.setBounds(328, 64, 100, 21);
        synchronized(this)
        {
            //Starts the timer, passing in the GUI
            new AlertTimer(this).start();
        }
    }
    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {

        try {
            GUITest3 window = new GUITest3();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    public void setTimerLabel(final String timeLeft)
    {
        //For changing the displayed time
        Display.getDefault().asyncExec(new Runnable() {
            public void run()
            {
                lstAlert_1.setText(timeLeft);
            }
        });
    }




}

class AlertTimer {
    private static final int TIMER_PERIOD = 1000;   //Sounts in one second intervals
    protected static final int MAX_COUNT = 300;     //Max count, a.k.a. five minutes
    private GUITest3 gui;
    private int count = 0;

    public AlertTimer(GUITest3 screenie) {
        gui = screenie;
        String text = (count / 60)+":0"+(count % 60);
        screenie.setTimerLabel(text);
    }

    public void start() {
        new Timer(TIMER_PERIOD, new ActionListener() {
            @Override
            public synchronized void actionPerformed(ActionEvent e) {
                if (count < MAX_COUNT) {
                    count++;
                    String text = "";
                    if((count % 60) > 9)
                    {
                        text = (count / 60)+":"+(count % 60);
                    }
                    else
                        text = (count / 60)+":0"+(count % 60);
                    System.out.println(text);
                    gui.setTimerLabel(text); 
                } else {
                    //((Timer) e.getSource()).stop();
                    Display.getDefault().syncExec(new Runnable() {
                        public void run()
                        {
                            synchronized (gui)
                            {
                                AlertFlash alrt1 = new AlertFlash(gui);
                                Thread flashing = new Thread(alrt1);
                                flashing.start();
                            }
                        }
                    });
                }
            }
        }).start();
    }

}
Display.getDefault().syncExec(new Runnable() { // <- try changing it to asyncExec
    public void run()
    {
        synchronized (gui)                     // <-- try removing this
        {
            AlertFlash alrt1 = new AlertFlash(gui);
            Thread flashing = new Thread(alrt1);
            flashing.start();
        }
    }
});
class MyTimerTask extends TimerTask {
    @Override
    public void run() {
        count++;
    }
}

MyTimerTask timerTask = null;
int count = 0;

public void start() {
    count = 0;
    timerTask = new MyTimerTask();
    timer.scheduleAtFixedRate(timerTask, 1000, 1000);
}

public void stop() {
    if (timerTask != null)
        timerTask.cancel();
}