Swing多线程编程

    基本上所有GUI库都是单线程的,Swing就是一种GUI库。什么意思呢?就是说所有对UI的更新都是在主线程中进行,这也是Swing的EDT线程(事件派发线程)也被叫UI线程的原因。如果在UI线程中执行比较耗时的操作,界面会卡住。Swing有个单线程规范,只要牢记会避免很多大坑:所有界面操作的更新都应该在EDT线程执行,所有耗时的操作都应该在单独线程中执行。

     请牢记上面的红字部分,然后我们开始一个实例:点击按钮后,按钮显示秒数,每过一秒显示加1。下面是第一个版本

package com.albert.frame;

import java.awt.BorderLayout;

public class TestSwing extends JFrame {
	private JPanel contentPane;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestSwing frame = new TestSwing();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public TestSwing() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		final JButton button = new JButton("点击");
		contentPane.add(button, BorderLayout.CENTER);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
an				for(int i=0;i<=5;i++){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					button.setText(i+"");
				}
			}
		});
	}

}

 运行代码点击按钮后,会发现界面卡住了,然后过了5秒后,按钮上直接显示了5。而不是我们期望的从0开始每隔一秒加1。

      这是上面原因呢?很明显这个违背了上面的原则:不能在EDT线程中进行耗时操作。因为你的耗时操作占用了EDT线程,然后界面更新会去耗时操作后面排队,最后同一个组件的界面更新会合并成最后一个也就是直接显示5。需要说明的是Swing组件的各种事件监听方法都是在EDT线程调用的,比如单击事件。

      既然程序并没有按照我们的想法执行,是因为违背了GUI单线程的原则,那我们再次修改下程序,把耗时操作的代码加入到单独的线程中,只贴出修改的部分

button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					public void run() {
						for(int i=0;i<=5;i++){
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e1) {
								e1.printStackTrace();
							}
							button.setText(i+"");
						}
					}
				}).start();
			}
		});
}

运行程序并且点击按钮,发现程序按照我们的期望执行了。是不是很开心啊,可是不要高兴的太早,虽然功能实现了,但是还是违背了GUI单线程原则:在EDT线程中进行界面的更新。而我们在单独线程中运行了这段界面更新操作的代码:button.setText(i+"")。

       简直头大,button.setText()和我们的耗时操作是一个代码块怎么能分开在两个线程执行啊?日了狗日了!!其实Swing给我们提供了一个工具类SwingUtilities,里面有个方法invokeLater(Runnable runnable),这个方法的作用就是把一个runnable(就是一个任务,很多初学者学习线程认为Runnable也是线程的一部分,其实Runnable只是一个有一个run方法声明的接口)传到EDT线程中,让EDT线程去执行。再次修改代码

button.addActionListener(new ActionListener() {
			int i;
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable() {
					public void run() {
						for(i=0;i<5;i++){
							try {
								Thread.sleep(1000);
							} catch (InterruptedException e1) {
								e1.printStackTrace();
							}
							SwingUtilities.invokeLater(new Runnable() {
								public void run() {
									button.setText(i+"");
								}
							});
						}
					}
				}).start();
			}
		});
	}

 程序执行的结果也就是我们期望的,并且也符合了GUI单线程的原则,皆大欢喜了。

 然后介绍下Swing提供给我们类SwingWorker,在doInbackground()中执行耗时操作,然后返回结果,可以在done()方法中用get()获取到doInbackground()返回的结果进行界面刷新。

SwingWorker worker = new SwingWorker<Integer, Void>() {
				@Override
				protected Integer doInBackground() throws Exception {
					//耗时操作。。。
					
					return 0;
				}
				@Override
				protected void done() {
					super.done();
					//上面的耗时操作完成后把done()交给EDT执行。
				}
			};

好了,以上就是Swing的线程说明,希望有帮助

猜你喜欢

转载自zyqwst.iteye.com/blog/2262011