Learn Java Thread Learning notes

Source: Internet
Author: User


Threads: A branch of code execution
The main function is to improve the efficiency, the CPU can execute multiple parts of the code at the same time.
Thread creation: two ways
A. Inherit from the thread class, overriding the Run method.
B. Implement the Runable interface and implement the Run method.

The start thread must use the thread's Start method, or if the direct calling thread's run-side rule is single-threaded.
There is an object that starts two threads and two objects initiates two threads of difference: One object starts two threads, and the modified content in two threads is always for the same object. Two object launches
Two threads, the content modified in a thread is for different objects.

Thread class: Partial Methods
Start () causes the thread to start executing, and the Java virtual machine calls the thread's Run method.
GetPriority () returns the priority of the thread.
setpriority (int newpriority) takes precedence over the thread.
CurrentThread () returns a reference to the currently executing thread object.
SetName (String name) changes the name of the thread so that it is the same as the parameter name.

The state of the thread:
1. NEW: A thread is in a new state after a new thread
2. Ready: When a thread invokes the Start method, it is in a ready state (note that this method is too called and does not necessarily have to execute the thread immediately)
3. Run: The thread is running when executing the Run method
4. Sleep: The thread is resting after the sleep method is called (this state becomes ready immediately after the break time)
5. Wait: The thread waits after the wait method is called.
6. Death: The Run method is executed.
The difference between the wait state and the sleep time state:
Sleep is the time when nature is ready, and only the CPU is allocated an event slice that can be executed immediately.
The wait state is a waiting state that never wakes up if there is no thread notification.

Common methods:
A.currentthread (): Static method. Gets the current thread.
B.getname (): Gets the name of the thread
C.setname (): Sets the name of the thread (no setting has a default name)
D.sleep (): Let the thread rest for a while.
E.join (): Merges a thread, allowing two threads to merge one thread.
F.yeild (): Current thread yields CPU, current thread is in a ready state
G.setpriority (): Sets the priority of the thread. The default level is 5. Priority from 1~10;

Thread synchronization: Thread synchronization occurs when multiple threads execute a piece of code at the same time. The results are unpredictable at this time.
Prevent thread synchronization: Use the Synchronized keyword to decorate the code that needs to be synchronized.
Synchronized principle: is to add a lock on the resources, when a thread into this resource execution code, immediately lock, the other threads
Execution is in a wait state. Until this thread releases the lock, other threads can enter this resource execution code.
















Learn Java Thread Learning notes

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.