Reading C++ Primer, Fourth Edition.
Java Persistence with Hibernate

Reading Core Java, Volume 1: Fundamentals Eighth Edition

Linkou Bian posted @ Tue, 20 Oct 2009 18:43:43 +0800 in Reading notes with tags Core Java , 7575 readers

 October 20, 2009

Just bought Core Java, Volume 1: Fundamentals Eighth Edition. Have read chapter 1 and 2 yet.

I met with a new method in chapter 2 today, which is named EventQueue.invokeLater, so look up more usage about it. Swing event handling code runs on a special thread known as the event dispatch thread. Most code that invoke Swing methods also runs on this thread. This is necessary because most Swing object methods are not "thread safe":invoking them from multiple threads risks thread interference or memory consistency errors. Some Swing component methods are labeled "thread safe" in the API specification; these can be safely invoked from any thread. All other Swing component methods must be invoked from the event dispatch thread. Programs that ignore this rule may function correctly most of the time, but are subject to unpredictable errors that are difficult to reproduce.

You can update the GUI components in any thread. However, their behavior is not always correct if you do so. Using invokeLater ensures that the GUI threads performs changes to the GUI and draws them correctly. For example, 

EventQueue.invokeLater(new Runnalbe(){
   new JFrame().setVisible(true);
});

As we know, actionPerformed is called by EDT(short for Event Dispatch Thread), so you can update Swing component in this method directly. But if task in actionPerformed needs a long time, you may need a new thread and update related Swing component in this new created one. At this point, you should pay attention to the Swing component that you will manipulate. If the it is not "thread safe", you should use EventQueue.invokeLater!

Also, you can use SwingUtilities.isEventDispatchThread() to determin whether your code is running on EDT.

ps, here is an article about why we should not make Swing truly multithreaded. Have good day

 

 October 27, 2009

Have read chapter 3. 86 pages of this book have been read.

1. main function doesn't have any return statement, but you can use System.exit to return some state code to the OS

2. no unsigned type supported by Java

3. Java supports Chinese as variable name

4. Math.PI

5. s.equals(t) <==> s.compareTo(t) ==0

6. StringBuffer supports multiple threads, StringBuilder supports single threads

7. System.console().readPassword

8. BigInteger and BigDecimal

Avatar_small
AAA said:
Mon, 02 May 2022 04:10:26 +0800

I’d forever want to be update on new articles on this site . Selling Merchant Services

 

====================================

 

I like the way you conduct your posts. Merchant Account Affiliate Program

 

 

=========================================

 

 

I enjoy your writing type, do keep on writing! I’ll be back! Merchant Referral Program

Avatar_small
meidir said:
Thu, 21 Jul 2022 22:51:58 +0800

Great weblog here! Also your web site lots up very fast! What host are you the usage of? Can I am getting your affiliate hyperlink for your host? I want my web site loaded up as fast as yours lol. LED補光燈

Avatar_small
meidir said:
Wed, 24 Aug 2022 20:06:55 +0800

I like assembling useful information , this post has got me even more info! . acne treatment

Avatar_small
meidir said:
Thu, 25 Aug 2022 00:01:13 +0800

Hello I'm so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I'm here now and could the same as to express thank for a huge post and a all round entertaining website. Please do keep up the truly amazing work. 오피스타

Avatar_small
meidir said:
Sat, 03 Dec 2022 02:01:33 +0800

Top-notch post it is definitely. My boss has been searching for this content. 二手Macbook


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter