java 线程 事件,Java的事件派发线程解释

I've recently started learning and exploring the basics of GUI programming in Java.

Having been programming for a while I have only done backend work or work and as a result the closest I've gotten to user interfaces is the command console (embarrassing I know).

I'm using Swing and as far as I can gather that means by extension I am also using AWT.

My question is based on this piece of code:

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new frame.setVisible(true);

}

} );

I have been researching this for a while as I wanted to fully understand this strange piece of code and have come across the term 'Event-Dispatching Thread' multiple times. Correct me if I'm wrong but as I understand it; it has to do with using multiple threads and how Java Swing interprets those threads. I gather as well that the above code is used to make sure all the threads are 'safe' before it creates the window, hence the invokeLater?

I have read that:

"You can only call methods that operate on the frame from the Event-Dispatching Thread"

and that only under certain circumstances can you call methods that operate on the frame from the main method.

Can somebody please clarify to me what exactly the Event-Dispatching Thread is?

How it relates to multiple threads of execution and how those threads are not safe to be called from the main method? Also why do we need this invokeLater?

Can we not just create the window as any other object?

I've hit a bit of a road block in my research as I'm not grasping these relations and ideas.

A side note is that I like to base my knowledge on in-depth understanding as I believe this leads to the best overall outcome and as a result the best programs. If I understand in-depth how something works then you can use the tips and tweaks effectively rather than just parroting them back in to code, so please don't be afraid to give me some extra in-depth explanations and broaden my knowledge.

Thank you.

解决方案

The EventDispatching thread is a special thread that is managed by the AWT. Basically it is a thread that runs in an infinite loop processing event. The java.awt.EventQueue.invokeLater method is a special way to provide some code that will run on the event queue. Writing a ui framework that is safe in a multithreading environment is very difficult so the AWT authors decided that they would only allow operations on GUI objects to occur on a single special thread. All event handlers will execute on this thread and all code that modifies the gui should also operate on this thread.

Now the AWT does not usually check that you are not issues gui commands from another thread (The WPF framework for C# does do this). so it is possible to write a lot of code and be pretty much agnostic to this and not run into any problems. But this can lead to undefined behavior so the best thing to do is to always ensure that gui code runs on the event dispatcher thread. invokeLater provides a mechanism to do this.

So a classic example is that you need to run a long running operation like downloading a file. So you launch a thread to perform this action then when it is completed you will use invokeLater to update the UI. If you didn't use invokeLater and instead you just updated the ui directly you might have a race condition and undefined behavior could occur.

Also if you are curious why the awt authors don't just make the toolkit multithreaded here is a good article: http://weblogs.java.net/blog/kgh/archive/2004/10/multithreaded_t.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值