Java--the difference between the start and run methods in thread

Source: Internet
Author: User
Tags thread stop

First, know the thread of start () and run ()

1. Start ():

Let's take a look at the introduction of this method in the API:

Causes the thread to start executing, and the Java virtual machine invokes the thread's run method.

The result is two threads running concurrently, when the front thread (returned from the call to the start method) and another thread (executing its run method).

It is illegal to start a thread multiple times. This is especially the time when the thread has finished executing and cannot be restarted.

Start the thread with the Start method, and actually implement multi-threaded operation, without waiting for the Run method body code to complete and proceed directly to execute the following code. By invoking the start () method of the thread class to start a thread, the thread is in a ready (operational) state and is not running, and once the CPU time slice is taken, the run () method is started, where the method run () is called the thread body, which contains the contents of the thread to be executed. This thread terminates when the Run method finishes running.

2. Run ():

Let's take a look at the introduction of the method in the API first:

If the thread is constructed using a stand-alone Runnable running object, the method of the object is called, Runnable run Otherwise the method does nothing and returns.

    ThreadSubclasses should override this method.

The run () method is just a common method of the class, if you call the Run method directly, the program is still only the main thread of the threads, its program execution path is only one, or to execute sequentially, or to wait for the Run method body execution before you can continue to execute the following code, This will not achieve the purpose of writing threads.

3. Summarize:

The Start method is called to start the thread, and the Run method is just a normal method call to thread, or it executes in the main thread.

Second, the code example:

public static void Main (String args[]) {Thread t = new Thread () {public void run () {Pong ()}}; T.start (); System.out.print ("ping");} static void Pong () {System.out.print ("Pong");}

Output Result: Pingpong

public static void Main (String args[]) {Thread t = new Thread () {public void run () {Pong ()}}; T.run (); System.out.print ("ping");} static void Pong () {System.out.print ("Pong");}

Output Result: pongping

With the above two program instances, it is easy to distinguish between the start () method and the Run () method:

T.start (); This line of code is equivalent to starting a thread,

T.run (); This line of code is equivalent to using the Run method in the class T.

Third, Thread status description:

Thread state from a large aspect, can be summed up as: the initial state, operational status, non-operational state and extinction state, the specific can be subdivided into the 7 states shown below:

1) The implementation of the thread has two ways, one is to inherit the thread class, and the other is to implement the Runnable interface, but anyway, when we new thread instance, the thread enters the initial state;

2) When the object calls the start () method, it enters the operational state;

3) After entering the operational state, when the object is selected by the operating system, the CPU time slice will go into the running state;

4) After entering the state of operation, the case is much more, there is a general situation: when the. Run () method or the main () method ends, the thread enters the terminating state, and when the thread calls its own sleep () method or another thread's join () method, it goes into a blocking state that stops the current thread. But does not release the resources it occupies). When

After the end of sleep () or join (), the thread enters the operational state, waits for the OS to allocate time slices, and when the thread has just entered the operational state (note that it is not yet running), discovers that the resource to be called is locked (synchroniza,lock) and will immediately enter the lock pool state. Wait for the lock tag (the lock pool may already have other lines

Process is waiting to acquire the lock tag, when they are in the queue state, on a first-come-first-served basis, once the thread acquires a lock tag, it goes into the operational state, waits for the OS to allocate CPU time slices, and when the thread calls the Wait () method, it enters the waiting queue (which frees up all the resources in the State, After entering this state,

is not automatically awakened, you must rely on other threads to invoke the Notify () or Notifyall () method to be awakened (because notify () just wakes up a thread, but we are not sure which thread is specifically awakened, perhaps the thread we need to wake up to cannot be awakened, Therefore, in practice, the Notifyall () method is used to awaken

thread), the thread is awakened and enters the lock pool, waiting for the lock token to be acquired. When a thread calls the Stop method, the thread goes extinct, but because the stop method is unsafe and discouraged, you can implement the thread stop by using the conditions in the Run method.

Java--the difference between the start and run methods in thread

Related Article

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.