如何使用线程挂起Java中的执行 [英] how to suspend execution in java using thread

查看:183
本文介绍了如何使用线程挂起Java中的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式创建了一个向导.它包含3个面板.第二个是devicePane,第三个是detailsPane.第三面板由进度条组成.我希望我的程序在显示第三个面板后启动功能process()吗?有可能使用线程吗?

i have created a wizard programatically. it contain 3 panels. the second one is devicePane and third one is detailsPane. the third panel consist of a progress bar. i want my program to start a function process() after displaying the third panel? is it possible using thread?

else if(ParserMainDlg.this.POSITION==1){
    if(sqlConnectionPane.executeProcess()==true){    
        devicePane.setDeviceList();                             
         ParserMainDlg.this.POSITION++;
         fireStateChanged(oldValue);
    }
}
else if(ParserMainDlg.this.POSITION==2){
    System.out.println("position:"+ParserMainDlg.this.POSITION);
    if(devicePane.executeProcess()==true){
         ParserMainDlg.this.POSITION++;
         fireStateChanged(oldValue);    
    }

我要sqlConnectionPane.executeProcess()调用一个在显示devicePane面板后开始执行的函数吗?

I want sqlConnectionPane.executeProcess() to call a function which starts executing after displaying the devicePane Panel?

推荐答案

您可以明确地使用线程来执行任务,这是处理长时间运行任务的首选方式.

You can definitly use a thread to execute your task, this is the preferred way of handling a long running task.

您在这里有多个选择.所有选项都包括对向导进行回调,以更新进度条.

You have multiple options here. All options include making a callback to your wizard, to update the progressbar.

您可以使自己的任务类完全做到这一点,或者可以使用现有的

You can make your own task class wich does exactly this, or you can use the existing SwingWorker. "SwingWorker itself is an abstract class; you must define a subclass in order to create a SwingWorker object; anonymous inner classes are often useful for creating very simple SwingWorker objects."

使用我们刚刚了解的摇摆工人,可以使用类似以下的方法:

Using the swing worker we just learned about you can use something like this:

SwingWorker<Integer, Integer> backgroundWork = new SwingWorker<Integer, Integer>() {

        @Override
        protected final Integer doInBackground() throws Exception {
            for (int i = 0; i < 61; i++) {
                Thread.sleep(1000);
                this.publish(i);
            }

            return 60;
        }

        @Override
        protected final void process(final List<Integer> chunks) {
            progressBar.setValue(chunks.get(0));
        }

    };

    backgroundWork.execute();

请注意,您必须将任务分解成较小的部分,才能真正显示进度.

Note that you will have to break your task down into smaller parts to actually be able to display progress.

这篇关于如何使用线程挂起Java中的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆