How to Use threads
The Java platform was designed as a multi-threaded environment from the very beginning. When your main program is executed, other jobs, such as fragment collection and event processing, are performed in the background. Essentially, you can think of these jobs as threads. They are System Management threads, but in any case they are threads. Threads enable you to define independent jobs and do not interfere with each other. The system will switch these jobs into or out of the CPU, so that (from the external perspective) They seem to run at the same time.
When you need to process multiple jobs in your program, you can also use multiple processes. These processes can be created by yourself, and you can manipulate system threads.
You need to use several different classes or interfaces to process these multiple jobs:
Java. util. Timer class
Javax. swing. Timer class
Thread class
Runnable interface
For simple jobs, they usually need to be repeated. You can use the java. util. Timer class to tell it to "do it once every half a second ". Note: Most system routines use milliseconds. Half a second is 500 milliseconds.
The task that you want Timer to implement is defined in the java. util. TimerTask instance. The running method includes the task to be executed. These are demonstrated in the Hi class, where the string "Hi" is repeatedly displayed on the screen until you press Enter.
- ImportJava. util .*;
-
- Public ClassHi {
- Public Static VoidMain (StringArgs [])
- ThrowsJava. io.IOException{
- TimerTaskTask =New TimerTask(){
- Public VoidRun (){
- System. Out. println ("Hi ");
- }
- };
- TimerTimer =New Timer();
- Timer. schedule (tasks, 0,500 );
- System. Out. println ("Press ENTER to stop ");
- System. In. read (New Byte[10]);
- Timer. cancel ();
- }
- }
Java Runtime Environment works by running the program as long as one thread is running. In this way, when the cancellation is called and no other threads are running, the program exits. Some system threads are running, such as the fragment collection program. These System threads are also called background threads. The background thread does not affect the running environment. Only non-Background threads ensure that the running environment is not disabled.
Javax. swing. Timer class and java. util. timer class work in a similar way, but there are some differences that need to be noted. First, the running job is defined by the implementation of the ActionListener interface. Second, job execution is performed inside the event processing thread, unlike the java. util. Timer class. This is important because it relates to how Swing component sets are designed.
If you are not familiar with Swing, it is a set of graphical components that can be used by Java programs. Swing is called a single thread. This means that access to the internal content of the Swing class must be completed in a single thread. This specific thread is the event processing thread. In this way, for example, if you want to change the text of the Label component, you cannot just call the setText method of Jlabel. On the contrary, you must confirm that the setText call occurs in the event processing thread, which is exactly where javax. swing. Time class comes in handy.
To illustrate the second case, the following program displays the value of an added counter. The value of the US half-second counter increases and the new value is displayed.
- ImportJavax. swing .*;
- ImportJava. awt .*;
- ImportJava. awt. event .*;
-
- Public ClassCount {
- Public Static VoidMain (StringArgs []) {
- JFrameFrame =New JFrame();
- Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
- ContainerContentPane = frame. getContentPane ();
- Final JLabelLabel =New JLabel("",JLabel. CENTER );
- Label. setFont (New Font("Serif ",Font. PLAIN, 36 ));
- ContentPane. add (label,BorderLayout. CENTER );
- ActionListenerListener =New ActionListener(){
- IntCount = 0;
- Public VoidActionreceivmed (ActionEventE ){
- &