Use JAVA to implement thread wait prompt box

Source: Internet
Author: User

 


Java has become the world's most popular language in just five years since its birth. Java programmers are becoming the highest paying employees among other IT programmers. All of this is due to Java's good features: simplicity, object-oriented, distributed, platform-independent, portability, and multithreading. This article uses the multi-thread feature of Java to implement the thread wait prompt box.

1. Question proposal

In Java application programming, you sometimes need to process some transactions that occupy a large amount of system resources and take a long time in the GUI (graphical user interface). For example: mass Data exchange with databases, complex operations on large data volumes, remote connection to servers, and so on. When the system processes these transactions, if the thread where the GUI is located is still used, the interface will be frozen and cannot be refreshed. It seems that the system has crashed, this is a situation where a good software system is not allowed.

2. Solution

The solution to the above problem is to use the multi-thread feature of Java to open a separate thread for these time-consuming and resource-consuming transactions, and the prompt box "running, please wait" appears at the GUI ", the prompt box is automatically closed when the thread ends. This avoids interface freezing and ensures thread security. It is a good choice for software developers.

3. Specific implementation
(1) Example
Here is a simple example to illustrate how to use JAVA to implement the thread wait prompt box.
In this example, a simple GUI is implemented. The root form testFrame is a JFrame (framework) class. Place a JPanel: testPanel in testFrame, and then place a JButton ): add testButton to testPanel.
Press testButton and the system starts to run a simulated time-consuming and resource-consuming transaction. The transaction is displayed from 1 to 100000 on the standard output device, and the prompt box "the thread is running" appears, once the transaction is completed (that is, the thread ends), the prompt box is automatically closed.
(2) Implementation Method
To achieve the above functions, we can achieve this:
When you press the button, start a new thread to complete the transaction, that is, display from 1 to 100000 on the standard output device (implemented by the TestThread class in the Code ), then start another thread to display the prompt box "the thread is running" (implemented through the ThreadDiag class in the Code ).
To enable the prompt box to be closed after TestThread ends, a DisposeDiag thread is started after TestThread starts. This thread is used to wait until the end of the TestThread thread, close the prompt box "the thread is running.
(3) program code and comments
① TestFrame class
TestFrame is the main program running in Java and used to display the user interface.

Import javax. swing .*;
Import java. awt .*;
Import java. awt. event .*;
Public class TestFrame extends JFrame
{
// Components required by the GUI
Public JPanel testPanel = null;
Public JButton testButton = null;
Public JFrame testFrame = null;
Public TestFrame ()
{
// Set the GUI to a windows style
Try
{
UIManager. setLookAndFeel (
"Com. sun. java. swing. plaf. windows. WindowsLookAndFeel ");
}
Catch (Exception ex)
{
System. out. println ("Exception:" + ex );
}
TestFrame = this;
// Initialize the GUI
Dimension dimensions = Toolkit. getdefatooltoolkit (). getScreenSize ();
SetSize (dimensions. width/2, dimensions. height/2 );
SetLocation (dimensions. width/2-dimensions.width/4,
Dimensions. height/2-dimensions.height/4 );
TestPanel = new JPanel ();

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.