Thread Knowledge Point Summary

Source: Internet
Author: User
Tags thread class

One, there are two ways to create threads: inheriting the thread class and implementing the Runnable interface

1. Inheriting thread, the Run method needs to be rewritten because the Run method in the thread class does nothing. If you want to open a new thread, it is not enough to create the thread object, only the start () method of the thread object is executed to open the new thread in the stack and execute the Run () method. If you do not call the start () method, just call the Run () method with Threaddemo, there is no difference between calling a function with a generic object and not opening a new thread.

public class Threaddemo extends thread{

public void Run () {

Thread Task Area

}

}

2, implement the Runnable interface, just create the thread task, but also need to create the thread object. The thread task object is used as a parameter when the thread object is created, and then the start () method of the thread object is called to open the thread and invoke the run () method of the thread task.

public class Runnablesub implements runnable{

public void Run () {

Thread Task Area

}

}

Second, the state of the thread

Thread blocking state (with CPU execution qualification, but no CPU execution)

|sleep () time over, call notify ()/notifyall () method

|

The thread state is created-----------------------start ()---------------------------thread runs-----------wait (), sleep ()----------------- ------Thread freezes state (frees the CPU for execution and execution)

(with CPU execution qualification and execution rights)

|

| (call the Stop () method, this method is obsolete; or the thread task has been executed; using tags)

Thread Destruction

Three, wait () and sleep () the difference

Same point: All can be threads in a frozen state

Differences: 1. The wait () method can only be used in a synchronous code block and frees the CPU for execution and execution; the sleep () method, which can be used in a synchronous code block or outside the synchronization code block, does not release the lock if used in a synchronous code block. The CPU is freed from execution and execution, the wait time is over, and the execution of the CPU is resumed. If you are synchronizing a block of code, you will release the CPU's execution and execution rights.

2. The wait () method needs to be awakened by the Notify ()/notifyall () method, and the sleep () method becomes blocked from the frozen state when the time is past.

Iv. the difference between synchronous code block and synchronous function (synchronization code block is differentiated by lock)

The same point: two are all designed to ensure thread safety.

Difference: Synchronous code block uses synchronized (Sync Lock) {

A synchronization lock is an object of any class, because the object class contains a method for the monitor, so all classes have a method for the monitor.

Code to process shared data

}

The synchronization function is on the function type, the lock is the object of the current class (this)

Public synchronized void SetName (name) {

This.name=name;

}

V. Determine if synchronization is required

1, whether to include shared data (if you do not include shared data, you do not need to use synchronization);

2. Is there more than one code to handle shared data?

Vi. Steps to address thread safety issues

1, first determine whether the need for synchronization;

2. There is a loop in the run () method;

VII. Threading Model

1, a single producer and consumer;

2, multi-producer and multi-consumer;

Viii. Concept of thread groups

To facilitate the management of threads, if the thread is in suspended animation, you can use the thread to invoke the interrupt () method, force the shutdown of the thread, and, if there are multiple, call this method multiple times, and if multiple threads are in the same thread group, you can use the thread group to call the interrupt () method. is equivalent to multiple threads calling the interrupt () method.

Nine, the difference between the foreground thread and the waiting thread

Threads that are created directly are foreground threads that need to wait for the thread to run to finish before the thread shuts down properly;

The foreground thread can turn the foreground thread into a waiting thread by calling the method, and if the foreground thread is finished, the waiting thread will shut down automatically (whether or not it is finished).

X. Priority of Threads

Level 1-10, the higher the number the higher the priority, the default priority is 5, the main thread is also 5

Xi. Join () method, Yelid () method

If the join () method is called in the main thread, the main thread waits for the current thread to finish before it resumes execution;

This method is similar to sleep (), except that the user cannot specify how long to pause, does not release the lock, and uses Thread.yelid () in the Run () method;

Thread Knowledge Point Summary

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.