线程返回true后继续

时间:2016-03-22 21:28:39

标签: java multithreading wait jprogressbar

在线程完成之前,我无法让代码“暂停”。我的程序只是一个框架内的JProgressBar。我正在使用一个位于线程内部的循环,它每10毫秒循环一次并向进度条添加+1。最终结果将使条形看起来是动画的,因为它的值从0增加到100.

现在,问题是让代码“等待”,直到进度条达到值100.正如您在代码中看到的那样,我希望仅在时重新启用按钮进度条达到值100.截至目前,随着进度条的增加,按钮似乎会同时重新启用。

    disableButtons();

    // loop and animate the progress bar
    for (int i = 0; i <= 100; i++) {
        Game.setProgBar_loading(i);

        // wait a number of milliseconds
        try {
            Thread.sleep(10);

        } catch (InterruptedException e) {}

    }

    enableButtons();

3 个答案:

答案 0 :(得分:1)

您可以在Loading中嵌套Program课程,然后从那里访问Program个变量,然后复制这部分代码:{{1}进入循环的末尾。像这样:

// <TODO> re-enable all buttons

编辑:要概括您的class Program { // assuming you have these buttons: private JButton button1; private JButton button2; private JButton button3; // snip public Program() { // assuming there's initialization code for the above buttons somewhere... // button1 = new ... // button2 = new ... // button3 = new ... // frame initializing setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setBounds(100, 100, 890, 480); getContentPane().setLayout(null); setVisible(true); // create the loading bar loadingBar = new JProgressBar(); loadingBar.setBounds(10, 11, 864, 23); getContentPane().add(loadingBar); // <TODO> disable all buttons button1.setEnabled(false); button2.setEnabled(false); button3.setEnabled(false); // animate the loading bar Loading thread = new Loading(); new Thread(thread).start(); // do not re-enable the buttons here yet. } // snip public class Loading implements Runnable { @Override public void run() { // loop and increment loading bar's value for (int i = 0; i <= 100; i++) { Program.setProgressBar(i); try { Thread.sleep(10); } catch (InterruptedException e) {} } // re-enable the buttons here instead button1.setEnabled(true); button2.setEnabled(true); button3.setEnabled(true); } } } 类并稍后重复使用,您可以创建一个包含LoadingbeforeExecute()等方法的界面。将该接口作为afterExecute()构造函数的参数并将其保存为成员,以便稍后可以在Loading内调用接口的方法:

run()

然后,在你的GUI线程中你会做这样的事情:

public interface LoadingHandler {
    public void beforeExecute();
    public void afterExecute();
}

public class Loading implements Runnable {
    private LoadingHandler handler;

    public Loading(LoadingHandler handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        // disableButtons();
        // use interface's methods instead:
        handler.beforeExecute();

        // loop and animate the progress bar
        for (int i = 0; i <= 100; i++) {
            Game.setProgBar_loading(i);

            // wait a number of milliseconds
            try {
                Thread.sleep(10);

            } catch (InterruptedException e) {}

        }

        // enableButtons();
        // use interface's method instead:
        handler.afterExecute();
    }
}

答案 1 :(得分:0)

为什么不在加载栏达到100%后启用按钮?您可以通过回调来执行此操作,也可以将相关代码放在setProgressBar方法

// set the progress bar's value
public static void setProgressBar(int num) {
    loadingBar.setValue(num);
    if(num >=100){
       // <TODO> re-enable all buttons
    }
}

答案 2 :(得分:-1)

如果不同时执行并行操作,我不能理解为什么需要使用线程来设置进度条的动画。

加入方法从Thread类开始阻塞,直到线程执行终止。

...
Thread animatedProgressBarThread = new Thread(thread);
animatedProgressBarThread.start();
// next line blocks until thread terminates its execution
animatedProgressBarThread.join();

正在加载类的运行方法使用Catch块来忽略InterruptedException。根据文档,创建的线程不会被中断。 catch块应该将线程的状态设置为中断并终止。