例子线程Java

问题描述 投票:0回答:1

为什么第一个例子不能用,而第二个例子用线程能用?我不明白睡眠有什么用。它是一个计数器,在按下启动按钮后开始计时。如果我按下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个回答
1
投票

Thread.sleep(500) 方法使主程序线程完全停止500毫秒。因为在第一个例子中,你是在一个线程中做所有的事情,所以程序只是冻结了,因为你调用了 Thread.sleep(500) 反复执行。

在第二个例子中,主线程一直在运行,而你的Counter线程在睡觉。因此,你的程序不会冻结,一切都能正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.