Multithreading (J2SE learning essay three)

Source: Internet
Author: User
Tags sleep thread class
Multithreading

1. A thread is an execution path in a process. A process is a program that is running in the system. (Access is a static concept, and threading is a dynamic concept.) )

2. How to open a new thread:

A, inherit the thread class, and override the Run method in thread.

b, implement the Runnable interface.

Note: Opening a new thread is by calling the Start method, and calling the Run method directly is only a generic method call.

3, the transition of the thread state:

4, the basic method of threading control

IsAlive () Determines whether the thread is still alive, that is, whether the thread has not been terminated.

GetPriority () Gets the priority number of the thread.

SetPriority () Sets the priority number of the thread.

Thread.Sleep () See the current thread sleep for a specified number of milliseconds.

Join () calls the method of a thread, merges the current thread with the thread, waits for the thread to end, and then replies to the current thread's run.

Yield () yields the CPU, and the current thread enters the ready queue for scheduling. (Let only once)

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

Notify ()/notifyall () wakes up one/all waiting threads in the wait pool of the object.

Note: The priority of a thread is a range of 1 to 10, and the default is 5.

5, thread synchronization is to prevent multiple threads from accessing the same data object, the data damage.

Ways to implement thread synchronization:

A, code block lock:

Synchronized (this) {...}

B, Method Lock:

Public synchronised void XXX (...) {......}

6, Thread deadlock: a rare, and difficult to debug error, when two thread objects have a circular dependency on two synchronization objects, there will be a deadlock phenomenon.

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.