Java thread (2): Creation and startup

Source: Internet
Author: User

Java Thread: Create and start SCJP5 learning Notes 1. Define Thread 1. Extend java. lang. Thread class. There is a run () method in this class. Pay attention to its usage: public void run (). If this thread is constructed using an independent Runnable running object, the run method of the Runnable object is called; otherwise, this method does not perform any operations and returns. The subclass of Thread should override this method. 2. Implement the java. lang. Runnable interface. When void run () creates a thread using an object that implements the interface Runnable, starting this thread will result in calling the run method of the object in an independently executed thread. The common protocol for method run is that it may execute any required operations. Ii. instantiate Thread 1. If it is a Thread that extends the java. lang. Thread class, it can be new directly. 2. If java is implemented. lang. the Runnable interface class is constructed using the Thread method: Thread (Runnable target) Thread (Runnable target, String name) Thread (ThreadGroup group, Runnable target) Thread (ThreadGroup group, runnable target, String name) Thread (ThreadGroup group, Runnable target, String name, long stackSize) 3. the startup Thread calls the start () method on the Thread object instead of the run () method () or other methods. Before calling the start () method: the Thread is in a new State. The new State indicates that there is a Thread object, but no real Thread exists. After the start () method is called: a series of complex tasks occur to start a new execution thread (with a new call stack); the thread is transferred from the new state to the runable state; when the thread gets the opportunity to execute, its target run () method will run. Note: For Java, the run () method has nothing special. Like the main () method, the new thread only knows the name (and signature) of the called method ). Therefore, it is legal to call the run method on Runnable or Thread. But it does not start a new thread. Iv. Example 1. multi-threaded example for implementing the Runnable interface/*** class for implementing the Runnable interface ** @ author leizhimin 18:12:10 */public class DoSomething implements Runnable {private String name; public DoSomething (String name) {this. name = name;} public void run () {for (int I = 0; I <5; I ++) {for (long k = 0; k <100000000; k ++); System. out. println (name + ":" + I) ;}}/*** test the multi-threaded program implemented by the Runnable class ** @ author leizhimin 2008-9 -13 18:15:02 */public class TestRunnable {public static void main (String [] args) {DoSomething ds1 = new DoSomething (""); doSomething ds2 = new DoSomething (""); Thread t1 = new Thread (ds1); Thread t2 = new Thread (ds2); t1.start (); t2.start ();}} execution result: LI 4: 0 A 3: 0 LI 4: 1 A 3: 1 Li 4: 2 LI 4: 3 A 3: 2 LI 4: 4 A 3: 3: 4 Process finished with exit code 0 2. multi-Thread example of extended Thread class implementation/*** test the multi-Thread program of extended Thread class implementation ** @ Author leizhimin 18:22:13 */public class TestThread extends Thread {public TestThread (String name) {super (name);} public void run () {for (int I = 0; I <5; I ++) {for (long k = 0; k <100000000; k ++); System. out. println (this. getName () + ":" + I) ;}} public static void main (String [] args) {Thread t1 = new TestThread ("A 3 "); thread t2 = new TestThread ("Li Si"); t1.start (); t2.start () ;}} Result: A 3: 0 LI 4: 0 A 3: 1 Li 4: 1 A 3: 2 LI 4: 2 A 3: 3 A 3: 4 LI 4: 3 LI 4: 4 Process finished with exit code 0 the output results of the preceding multi-threaded program code are uncertain. One of the statements for (long k = 0; k <100000000; k ++) is used to simulate a very time-consuming operation. 5. Some Common Problems 1. The thread name. A running thread always has a name, and the name has two sources. One is the name given by the virtual machine itself, one is your own name. If no thread name is specified, the virtual machine always specifies the thread name, and the name of the main thread is always mian. The name of a non-main thread is unknown. 2. You can set the thread name or obtain the thread name, which is no exception to the main thread. 3. The method for obtaining the object of the current Thread is: Thread. currentThread (); 4. In the above Code, it can only be ensured that each thread will be started, and each thread will run until it is completed. Starting a series of threads in some order does not mean they will be executed in this order. For any group of started threads, the Scheduler cannot guarantee the execution order and duration. 5. When the thread's target run () method ends, the thread completes. 6. Once the thread starts, it will never be restarted. Only one new thread can be started once. A running or dead thread can be restarted. 7. Thread Scheduling is part of the JVM. On a CPU machine, only one thread can be run at a time. Only one thread stack is executed at a time. The JVM thread scheduler determines which thread is running. One of the many runnable threads will be selected as the current thread. The sequence in which a running thread is selected is not guaranteed. 8. Although queues are usually used, this is not guaranteed. The queue Mode means that when a thread completes a "Round", it will move to the end of the runable queue and wait until it finally queues to the front end of the queue before it can be selected again. In fact, we call it a runable pool rather than a runable queue to help us understand the fact that not all threads are arranged in a certain order and sing a queue. 9. Although we have not been unable to control the thread scheduling program, we can influence the Thread Scheduling Method in other ways.

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.