Java multithreaded Learning (iii)--thread stacks

Source: Internet
Author: User

One, line stacks model

The line stacks model is the basis for understanding the thread scheduling principle and the thread execution process. Line stacks refers to the stack of memory thread scheduling at a certain time, the current method is always at the top of the stack, the content of the line stacks changes with the running state of the threads, and the research thread stack must choose a running moment (where the code runs)


The stack A in the main thread is the run stack information when executing the new Javathreaddemo (). Threadmethod () method, the Threadmethod method is located at the top of the main line stacks, and the start is run in the Threadmethod method. () method creates a new thread, at this point, the newly established thread will also have its own line stacks B, you can see that the information at different runtime stacks is changing, and stack a changes can be seen from there. At this point stack a and stack b run in parallel, and the main thread runs in parallel with the newly created thread. This allows you to see the difference between method invocation and thread initiation. The method call just stacks the code of the method called in the original line, and the thread initiation will create a new thread that belongs to itself Cheng run its own threads.

Second, the life cycle of the thread

The life cycle of a thread consists of five states: new, operational, running, blocked, dead.


NEW: A new Thread object was created, but the start () method is not yet called in the thread, and the thread is in the new state.

Operational status: The Start () method of the calling thread, the thread goes into a running state, and the thread waits for the JVM Scheduler to execute, that is, I am ready to go to the battlefield, waiting for only one chance to become operational.

Running state: The thread Scheduler chooses a thread from a multitude of running state threads to run, as the manager chooses a big future to battle. This is also the only way for threads to enter the running state, which must be dispatched by the JVM.

Blocking states: Threads Wait, sleep, and block are collectively called blocking states, where the thread is still alive and on standby, knowing that a condition appears and can return to a running state.

Dead state: When the thread's run () method finishes executing, the thread ends. At this point the thread is no longer present, and all of the resources it consumes are recycled.


Third, thread blocking

There are a number of threads that are blocked, including three items (IO blocking is not discussed)

1. Sleep

2. Wait

3. Get the thread lock and block

Sleep state: The thread's Sleep () method allows a thread that is executing to go to sleep. When a thread sleeps, it does not return to a running state and returns to a running state when the sleep time is reached.

@Overridepublic void Run () {for (int i = 0; i < 5; i++) {try {thread.sleep (50);//analog time-consuming operation SYSTEM.OUT.PRINTLN (name + ":" + I );} catch (Interruptedexception e) {e.printstacktrace ();}}}
The static method of the calling thread, Thread.Sleep (), can cause the thread to sleep with a parameter of milliseconds.

1. When the thread is asleep, the JVM scheduler pauses execution of the thread and executes other threads that are in a running state.

2. The sleep () method is a static method of thread and can only control the sleep of the current thread.

3, the thread sleep time will be returned to the operational state, not the running state.

Thread priority and thread concession yield ()

Threads implement thread concessions by calling the Thread.yield () method. The yield () method acts as a pause for the currently executing thread object, yielding the processor resources and allowing the processor to execute other threads.

Threads in Java have a priority, with a range of 1--10, between the priority levels. The JVM's thread scheduler is a priority-based preemptive scheduling mechanism. A higher-priority thread has a higher chance of being executed than a low-priority thread, but the odds are higher, not that the low-priority thread must wait for a high-priority thread to execute before it executes, but the proportion of resources allocated is different. (Thread priority does not work as you expect in real-world applications)

When a thread in a thread pool has the same priority, the JVM scheduler randomly chooses a thread that is in a running state to execute, and there may be two kinds of execution: (1) Select a thread to run until the thread is blocked or run to death. (2) Time fragmentation mechanism, which provides a uniform running opportunity for each thread in the thread pool.

Setting the priority of a thread can set the thread's priority by calling the thread's setpriority (int) method. The default thread priority is 5,JVM does not arbitrarily change the priority of a thread, but the range of thread priorities in different systems may be different, and when the JVM does not recognize 10 different priorities, the JVM will merge those priorities two or more. At this point, the different priority threads before the merge are eventually merged into a priority thread.

The thread class defines three constants to define the range of precedence:

static int max_priority thread has the maximum precedence

static int min_priority The minimum priority that the thread has

static int norm_priority thread has the default precedence


Yield () method that gives the thread a concession

The Thread.yield () method pauses the currently executing thread, giving the processor the opportunity to execute other threads, which can allow the current thread to make concessions and allow other threads to be executed. After the thread executes the yield () method, the thread is returned from the running state to the operational state, allowing the thread with the same priority to get an opportunity to execute. Use the yield () method to perform the appropriate rotation between threads of the same priority. However, in practice there is no guarantee that the yield () method will yield, because the compromised thread is returned from the running state to the operational state, and the thread can be selected for execution by the JVM's scheduling mechanism.


Join method

The non-static method of thread join () lets one thread B join the tail of another thread A, until thread a finishes executing, and thread B can continue. This is equivalent to interrupting thread B's execution to execute thread a first, similar to calling another function in the main () function, until the function finishes executing the main () function.

The Join method has a version with a time parameter, and T.join (5000) lets the thread wait for 5000 milliseconds, and if it exceeds this time, it stops waiting and becomes a running state.


Three ways to leave the thread temporarily out of the running state:

1. Call the Sleep () method of the thread so that the thread sleeps for some time

2. Call the yield () method of the thread so that the thread can temporarily return to the operational state, giving other threads the opportunity to execute.

3. Call the thread's join () method so that the current thread stops executing, knowing that the current thread can execute after the thread that is joined in the current thread finishes executing.






Java multithreaded Learning (iii)--thread stacks

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.