Multi-line threads worker thread pattern and swing

Source: Internet
Author: User

Recently at leisure to see "Java Multithreaded Design pattern", very good, language is easy to understand, and choreography is good, very enlightening, now pick one of the chapters to write ideas

Worker thread is a lot of multithreaded mode we usually use, but we often don't take it as a pattern. The basic content is: There is a pipeline (channel), the line has a client thread client, the other end of the worker thread worker, the customer constantly put the new task (request) into the assembly line, workers on the other side to get the task, and implementation, the number of customers and workers can be much less, It's that simple.

This so-called pattern looks like déjà vu, is a thread pool, according to the usual practice, request can implement runnable interface, the things to do in the Run method, by the worker to carry out, the specific implementation should also pay attention to the problem of synchronization

However, we can think of the way swing works. Swing is an event-driven, it has a event-dispatch queue, this is used because this queue is unique, and it is the same as the pipeline mentioned above. The various components of swing are the same as customers, constantly putting events (keyboard or mouse events, etc.) into the event queue, the queue has a dedicated thread responsible for these events to the corresponding listener, the implementation of the most basic event-driven model. If this model is not used, that is, the event is handled by a specialized thread, the corresponding speed of the interface is very poor

If you've ever used Java to play games, you should have been exposed to the Javax.swing.SwingUtilities class, which has a invokeandwait method that lets other threads manipulate the swing component. Why not direct operation, as mentioned above, the event dispatch queue is unique, so the swing component is designed without too much consideration for multi-threaded issues, anyway, by the event dispatch Queue Unified operation, This can improve speed (although swing is inherently slow), but when other threads are operating on swing components, there may be potentially destabilizing factors, so the Invokeandwait method is available, and the thread that calls this method will wait until the required operation is complete

The

is not finished, think again, we are often in the program of those components directly operate rather than with what invokeandwait, here are some details worth noting. First of all, before you call the setvisible and other methods of the component, you can arbitrarily reorganize the pieces, after calling the setvisible, only a few methods, such as Repaint,addlistener and so on. Finally, according to the JDK documentation, this method is used to change the appearance of the GUI for application threads. If you want to directly change, not necessarily error, multithreading is more difficult to say, oh, I have not tried, another day can try the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.