《Java Concurrency in Practice》ch9 GUI Applications

1. EDT (Event Dispatch Thread)

Question: Why are GUIs Single-threaded?

1) 不要再EDT执行时间较长的任务,否则GUI无法及时响应。

2) SwingUtilities.invokeLater() 可以将一个Runnable任务调度到事件线程中执行(可以在任意线程中调用)

2. 短时间的GUI任务

在EDT中处理

3. 长时间的GUI任务

使用SwingWorker,支持取消、进度标识和完成标识(可以考虑用于改正以前的假进度条了)。

下面是一个demo,from http://www.java2s.com/Code/Java/JDK-6/SwingWorkerfromJDK6SE.htm

import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

public class SwingWorkerDemo {
	public static void main(String[] args) {
		JTextArea textArea = new JTextArea(10, 20);
		final JProgressBar progressBar = new JProgressBar(0, 10);

		final CounterTask task = new CounterTask();
		task.addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				if ("progress".equals(evt.getPropertyName())) {
					progressBar.setValue((Integer) evt.getNewValue());
				}
			}
		});
		JButton startButton = new JButton("Start");
		startButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				task.execute();
			}
		});

		JButton cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				task.cancel(true);
			}
		});

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(startButton);
		buttonPanel.add(cancelButton);

		JPanel cp = new JPanel();
		LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS);
		cp.setLayout(layout);
		cp.add(buttonPanel);
		cp.add(new JScrollPane(textArea));
		cp.add(progressBar);

		JFrame frame = new JFrame("SwingWorker Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setContentPane(cp);
		frame.pack();
		frame.setVisible(true);
	}
}

class CounterTask extends SwingWorker<Integer, Integer> {
	private static final int DELAY = 1000;

	public CounterTask() {
	}

	@Override
	protected Integer doInBackground() throws Exception {
		int i = 0;
		int count = 10;
		while (!isCancelled() && i < count) {
			i++;
			publish(new Integer[] { i });
			setProgress(count * i / count);
			Thread.sleep(DELAY);
		}

		return count;
	}

	protected void process(List<Integer> chunks) {
		System.out.println("process : " + Thread.currentThread().getName());
		for (int i : chunks)
			System.out.println(i);
	}

	@Override
	protected void done() {
		System.out.println("done : " + Thread.currentThread().getName());
		if (isCancelled())
			System.out.println("Cancelled !");
		else
			System.out.println("Done !");
	}
}

运行结果:


process : AWT-EventQueue-0
1
process : AWT-EventQueue-0
2
process : AWT-EventQueue-0
3
process : AWT-EventQueue-0
4
process : AWT-EventQueue-0
5
process : AWT-EventQueue-0
6
process : AWT-EventQueue-0
7
process : AWT-EventQueue-0
8
process : AWT-EventQueue-0
9
process : AWT-EventQueue-0
10
done : AWT-EventQueue-0
Done !


可以看出,progress(在事件指派线程 上异步地从 publish 方法接收数据块)方法果然是被EDT线程调度的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值