Simple understanding of threading

Source: Internet
Author: User

    1. A thread is a different execution path within a program.
    2. the state of the thread: Create the Thread object first, and then call the start () method to enter the ready state. (But not immediately, wait until the CPU is scheduled to enter the running state, and then may be running when the problem, into the blocking state.) Could have been terminated.
    3. Some common methods:

Sleep (): Specifies how long the thread sleeps

Yield (): give the CPU to other threads

Notify (): Wake -Up thread

Notifyall (): Wake up All Threads

SetPriority (): Set Priority

    1. Two ways to implement threading:

Inheritance thread Threading class or implementing the runnable interface, it is recommended to implement the interface method, because java can not inherit more. Then rewrite their Run () method , which is similar to the execute () method inside the struts2 , Are all base class definitions that need to be implemented inside.

    1. thread synchronization:synchronized (this) {

code blocks that need to be synchronized;

}

Alternatively: Create a synchronization method by adding synchronized directly to the method.

Only the current code block runs out of other objects to access the current synchronization fast.

It is important to notethat the valid range of synchronized is only the current object or method under his control, and other non- synchronized objects, the method can still be accessed normally.

    1. Deadlock: When a thread needs an object to execute, but the object is locked by another thread, and the thread needs an object to execute, and the object is locked by you, it will cause a deadlock. The solution is to expand the block of code you want to synchronize.
    2. the difference between Wait () and sleep () :

First Wait () is the method of the object class, and sleep () is the thread class. Wait () is equivalent to letting go of the lock, other objects can access your original locked part, and sleep () is still locked, not accessible.

Simple understanding of threading

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.