Java knowledge Summary (3) -- thread

Source: Internet
Author: User

1. A thread is an execution path in a process. The process is running in the system.Program. (Inbound and outbound is a static concept, while a thread is a dynamic concept .)

2. How to start a new thread:

A. inherit the Thread class and override the run method in the thread.

B. Implement the runnable interface.

Note: To enable a new thread, you call the start method, but directly calling the run method is only a common method call.

3. thread state conversion:

4. Basic thread control methods

Isalive () determines whether the thread is still "alive", that is, whether the thread has not been terminated.

Getpriority () obtains the priority value of the thread.

Setpriority () sets the priority value of the thread.

For details about thread. Sleep (), see the specified number of milliseconds for the current thread to sleep.

Join () calls the method of a thread and merges the current thread with the thread. That is, it waits until the end of the thread and then returns to the running of the current thread.

Yield () gets out of the CPU, and the current thread enters the ready queue for scheduling. (Only once)

Wait () but its thread enters the wait pool of the object.

Y ()/notifyall () Wake up one/all waiting threads in the waitpool of the object.

Note: The thread priority ranges from 1 to 10. The default value is 5.

5. Thread Synchronization aims to prevent data corruption when multiple threads access the same data object.

Method for implementing thread synchronization:

A,CodeBlock lock:

Synchronized (this ){......}

B. Method lock:

Public synchronised voidxxx (...){......}

6. Thread deadlock: a rare and difficult-to-Debug error occurs when two thread objects have circular dependencies on two synchronization objects.

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.