To design concurrent programs, you must first have a sufficient understanding of multithreading. In JAVA, multithreading is usually implemented through the Thread class and Runnable interface. This article summarizes their usage.
1. Thread classIn java apidocs, the Thread class has the following: The Thread is the execution Thread in the program. Java Virtual Machine allows applications to run multiple execution threads concurrently. Each thread has a priority. The execution of a high-priority thread takes precedence over that of a low-priority thread. Each thread can or cannot be marked as a daemon. When the code running in a Thread creates a new Thread object, the initial priority of the new Thread is set to the priority of the Creation Thread, and only when the creation Thread is a daemon Thread, the new thread is the daemon. When a Java Virtual Machine is started, a single non-daemon thread usually calls the main method of a specified class ). Java virtual opportunity continues to execute the thread until any of the following circumstances occurs: the exit method of the Runtime class is called, and the security manager allows the exit Operation to occur. All threads of non-daemon threads have stopped running, whether returned by calling the run method or by throwing an exception outside the run method. This class must be used when creating a thread. This class encapsulates thread behavior.
2. Runnable interfaceThe explanation in the API is: the Runnable interface should be implemented by the classes that intend to execute its instance through a thread. Class must define a non-parameter method called run. This interface is designed to provide a public protocol for objects that want to execute code during activity. For example, the Thread class implements Runnable. Activation means that a thread has been started and has not been stopped. In addition, Runnable provides an activation method for non-Thread subclass classes. By instantiating a Thread instance and taking itself as the running target, you can run the Runnable class without creating a subclass of the Thread. In most cases, if you only want to override the run () method, instead of other Thread methods, you should use the Runnable interface. This is important because you should not create a subclass for the class unless the programmer intends to modify or enhance the basic behavior of the class. This explanation clearly describes the role of Runnable.
3. Create and start threads and precautions1) There are two ways to create a Thread in JAVA: 1) by inheriting the Thread class, you should override its run method and put the Thread running logic in it. Because the run method in Thread is an empty implementation. 2) Implement the Runnable interface, instantiate a Thread instance, and use itself as the running target. Which method should be used in use? Of course, either method works. However, the Runnable API documentation details how to choose the two methods: In most cases, if you only want to override the run () method, instead of rewriting other Thread methods, you should use the Runnable interface. This is important, because unless the programmer intends to modify or enhance the basic behavior of the class, it should not create a subclass for the class Thread. In the second method, internal classes are often used. 2) The start Thread cannot display the call because of the run method. To start a Thread, instead of calling the run method, it calls the start method of the Thread class. There are five ways to start a thread: Wen shaojin nickname: Wen Shao): // www.cnblogs.com/jobs /)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/141524L54-0.png "border =" 0 "alt =" thread Startup Mode "/>
Of course, the name is not required, because if no name is specified, the system will automatically generate a name in the form of "Thread-" + n, where n is an integer.
4. Use and description of important methodsIn the Thread class, some methods are often used. Apart from methods such as run and start, the static methods include sleep, join, interrupt, isInterrupted, and interrupted) wait, policy, policyall, and other methods inherited from the Object class are also frequently used. When an InterruptedException exception is thrown and how to handle the exception is related to interrupt, in the next article, I will introduce several other methods related to interruption in the use of multithreading-Interrupt Processing. In addition, methods such as resume, stop, and suspend have been abolished and are not recommended. We recommend that you use semaphores to share variables) or interrupt to replace the stop method. For more information, see the API documentation. Http://gceclub.sun.com.cn/Java_Docs/jdk6/html/zh_CN/api/index.html In addition, The currentThread method is usually used to get the current thread object in the run method, that is, the thread object that executes the run method ).
This article is from the "Xu Xinhua polaris" blog, please be sure to keep this source http://polaris.blog.51cto.com/1146394/371416