【问题标题】:Example Thread Java示例线程 Java
【发布时间】:2020-09-20 18:07:06
【问题描述】:

为什么第一个例子不起作用?而第二个带线程的作品?而且我不明白睡眠有什么用。它是一个计数器,按下启动按钮后启动。如果我按下 INC-DEC 按钮,它必须改变计数器趋势。所以它增加或减少计数器。

没有线程的Contatore.java

public class Contatore extends JFrame {
    private int count;
    private boolean runFlag;
    private JButton onOff;
    private JButton start;
    public Contatore() {
        runFlag = true;
        count = 0;
        setLayout(new FlowLayout());
        onOff = new JButton("INCR-DECR");
        add(onOff);
        onOff.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                runFlag = !runFlag;
            }
        });
        start = new JButton("START");
        add(start);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                while (true) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }(runFlag) count++;
                    else count--;
                    System.out.println(count);
                }
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    public static void main(String args[]) {
        Contatore f = new Contatore();
    }
}

带有线程的ContatoreRisolto.java

public class ContatoreRisolto extends JFrame {
    private boolean runFlag;
    private JButton onOff;
    private JButton start;
    public ContatoreRisolto() {
        runFlag = true;
        setLayout(new FlowLayout());
        onOff = new JButton("INCR-DECR");
        add(onOff);
        onOff.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                runFlag = !runFlag;
            }
        });
        start = new JButton("START");
        add(start);
        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start.setEnabled(false);
                Counter c = new Counter();
                c.start();
            }
        });
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    public static void main(String args[]) {
        ContatoreRisolto f = new ContatoreRisolto();
    }
    class Counter extends Thread {
        public void run() {
            int count = 0;
            while (true) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException exc) {
                    System.out.println(e.getMessage());
                }
                if (runFlag) count++;
                else count--;
                System.out.println(count);
            }
        }
    }
}

【问题讨论】:

    标签: java multithreading user-interface button listener


    【解决方案1】:

    Thread.sleep(500) 方法完全停止主程序线程 500 毫秒。因为在第一个示例中您在单个线程中执行所有操作,所以程序会因为您重复调用 Thread.sleep(500) 而冻结。

    在第二个示例中,主线程继续运行,而您的 Counter 线程处于休眠状态。因此,您的程序不会冻结,一切正常。

    【讨论】:

    • 谢谢,但我还是不明白为什么线程计数器必须休眠 500 毫秒。
    • 500 毫秒只是一个任意值。您可以选择任何数字,更新count 之间的时间会相应更改。
    猜你喜欢
    • 2017-11-05
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多