• java笔记使用SwingWoker类完成耗时操作


    使用SwingWoker类完成耗时操作:

    对于Swing中的耗时操作,通常要在一个新的线程中运行,以免程序"假死"。
    在java6.0中,可以用SwingWoker类来完成

    SwingWoker<T,V>是在专用线程中执行长时间GUI交互任务的抽象类。用Swing
    编写多线程应用程序时,要记住两个约束条件
        1.不应该在事件指派线程运行耗时任务,否则应用程序将无反应。
        2.只能在事件指派线程上访问Swing控件
        注: 通常需要将耗时的任务放到SwingWoker类的doInBackground()方法中
        执行:
           protected abstract T doInBackground()throws Exception

    --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3912080.html "谢谢--
       
    代码实例:

    package com.xhj.thread;
    
    import java.awt.Button;
    import java.awt.Label;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Arrays;
    import java.util.Random;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingWorker;
    
    /**
     * @author XIEHEJUN
     * 
     */
    public class UsedSwingWokerThread extends JFrame {
        private Label lable;
        private Button button;
    
        public Label getLable() {
            return lable;
        }
    
        public void setLable(Label lable) {
            this.lable = lable;
        }
    
        public Button getButton() {
            return button;
        }
    
        public void setButton(Button button) {
            this.button = button;
        }
    
        private class NumberArrary extends SwingWorker<Void, Integer> {
    
            @Override
            protected Void doInBackground() throws Exception {
                button.addActionListener(new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        int[] numArrary = new int[1000];
                        for (int i = 0; i < 1000; i++) {
                            numArrary[i] = new Random().nextInt();
                            System.out.println(i + "号数为:" + numArrary[i]);
                        }
                        Arrays.sort(numArrary);
                        System.out.println("生成的最大随机数为:"
                                + numArrary[numArrary.length - 1]);
                        lable.setText("生成的最大随机数为:"
                                + numArrary[numArrary.length - 1]);
                    }
                });
                return null;
            }
    
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            UsedSwingWokerThread usedSwingWoker = new UsedSwingWokerThread();
    
            JFrame frame = new JFrame();
            JPanel panel = new JPanel();
            frame.setContentPane(panel);
            frame.setVisible(true);
            usedSwingWoker.setLable(new Label("点击按钮生成随机数组"));
            usedSwingWoker.setButton(new Button("开始生成"));
            frame.add(usedSwingWoker.getLable());
            frame.add(usedSwingWoker.getButton());
    
            NumberArrary arrarys = usedSwingWoker.new NumberArrary();
            try {
                arrarys.doInBackground();
            } catch (Exception e) {
    
                e.printStackTrace();
            }
    
        }
    
    }

    注:SwingWoker的生命周期与线程
    1.当前线程:在该线程上调用execute()方法。它调度SwingWoker以在worker线程上执行并立即返回,
                可使用get方法等待SwingWoker完成。
    2.Worker线程:在该线程上调用doInBackground()方法。所有后台活动都应该在此线程上发生。
                  要通知PropertyChangListeners有关绑定(bound)属性的更改,请使用firePropertyChang
                  和getPropertyChangeSupport()方法、默认情况下,有两个可用的绑定属性,即state和progress
    3.事件指派线程:所有与Swing有关的活动都在该线程上发生。SwingWoker调用process和done()方法,
                    并通知该线程的所有PropertyChangListener。

    知识重在总结和梳理,只有不断地去学习并运用,才能化为自己的东西。由于本人进阶猿类时间尚短,故此博客即是我学习,工作的笔记,也是和大家交流,相互提升技术的平台~希望大家不吝赐教~~ --但管努力,莫问前程,事在人为,功不唐捐。--和佑博客园
  • 相关阅读:
    docker数据卷
    docker容器的启动、停止、运行、导入、导出、删除
    docker镜像的获取、创建、修改、删除、导入操作
    docker使用-spark安装
    python爬虫-3 解析库
    python爬虫-2 requests使用
    NLP-HMM
    NLP-中文分词-预处理
    python爬虫-1环境安装
    学习笔记3
  • 原文地址:https://www.cnblogs.com/XHJT/p/3912080.html
Copyright © 2020-2023  润新知