How to use threads

xiaoxiao2021-03-06  109

How to use the thread Java platform from the beginning to become a multi-threaded environment. Other homeworks such as debris collection and event processing are performed in the background when your main program is executed. Essentially, you can think that these homework are threads. They are just a system management thread, but in any case, they are threads. Threads allow you to define mutually independent jobs, mutual interference. The system will exchange these jobs or CPUs so that they seem to be running simultaneously. You can also use multiple processes when you need to handle multiple jobs in your program. These processes can be created by yourself, you can also manipulate system threads. You do these multi-job processing, use a few different classes or interfaces: java.util.Timer class Javax.swing.Timer class Thread class Runnable interface For simple jobs, you can usually need to repeat, you can use java.util. Timer class tells it "every half a second". Note: Most system routines are using milliseconds. Half-second is 500 milliseconds. You want Timer implementations to be defined in the Java.util.Timertask instance, where the running method contains the task to be executed. These are demonstrated in the HI class, where string "hi" is repeatedly displayed on the screen until you press Enter. Import java.util. *; public class hi {public static void main (string args []) throws java.io.ioException {timetask task = new timetask () {public void run () {system.out.println ("Hi ");}}; Timer Timer = new Timer (); time.schedule (task, 0, 500); system.out.println (" press enter to stop "); system.in.read (New Byte [10] ); Timer.Cancel ();}} Java Runtime Environment works by having a thread running, the program does not quit. This way, when the cancel is called, no other thread is running, then the program exits. There are some system threads running, such as debris collection procedures. These system threads are also referred to as background threads. The presence of the background thread does not affect the running environment is turned off, and only the non-enable thread guarantees that the operating environment is not closed. Javax.swing.Timer class is similar to the Java.util.Timer class, but there are some differences to pay attention. First, the operation is defined by the implementation of the ActionListener interface. Second, the execution of the job is done inside the event processing thread, not an external part of the Java.util.Timer class. This is very important because it is related to how the Swing component set is designed. If you are not familiar with swing, it is a set of graphics components that can be used by the Java program. Swing is designed as a single thread. This means that access to the Swing class internal content must be completed in a single thread. This particular thread is an event processing thread. This way, for example, you want to change the text of the Label component, you can't just call the Jlabel's setText method. Instead, you must confirm that the setText call occurs in the event handling thread, which is the place where the Javax.swing.Time class is used. To illustrate this second case, the following program displays the value of an increased counter. The numerical value of the US half-second counter increases, and the new value is displayed.

import javax.swing *;. import java.awt *;. import java.awt.event *;. public class Count {public static void main (String args []) {JFrame frame = new JFrame (); frame.setDefaultCloseOperation ( JFRAME.EXIT_ON_CLOSE); Container Contentpane = frame.getContentPane (); Final Jlabel Label = New Jlabel ("", JLabel.center); Label.SetFont (New Font ("Serif", Font.Plain, 36)); contentpane. add (label, BorderLayout.CENTER); ActionListener listener = new ActionListener () {int count = 0; public void actionPerformed (ActionEvent e) {count ; label.setText (Integer.toString (count));}}; Timer timer = New Timer (500, Listener); timer.Start (); frame.setsize (300, 100); frame.show ();}} The result of the above program is: If you don't have a simple repetitive job, Java.lang.Thread class is sent to the field. It allows you to control basic functions. By creating a subclass of Thread, you can make your system out and make a long-run job, such as reading a file from the network without hindering your other programs. This long-running job will be defined in the RUN method. The second way is to create a subclass of the Thread class and implement the RUN method in the subclass, or implement the RUN method in the category of runnable, and deliver this implementation to the constructor of Thread. You may ask what is the difference. Java programming languages ​​only support single inheritance. If your design is called other classes other than Thread, you can be your class to implement Runnable, and it can be your job being executed. Otherwise, you define the subclass of Thread to run your RUN method, no additional operations are added during the process. For the third case of creating the Thread subclass, the following program generates a new thread to calculate the number of characters of a specific URL, which is passed through the command line. In this process, implementing the fourth case of runnable is demonstrated, printing a repetitive message. Note that when you implement Runnable, you must provide the code for repeated messages. You must simultaneously SLEEP to allocate time and complete the operation. In both cases, compared to using Timer. The last part of this program contains you read commands from the command line to the end of the trigger. Note You always press ENTER to end the program while the system reads the URL and prints the message.

转载请注明原文地址:https://www.9cbs.com/read-125305.html

New Post(0)