Java multithreading Summary

Source: Internet
Author: User

Java multithreading Summary
Preface

Life's a climb, but the view is great.

1. Three ways to create a thread 1.1 inherit from Thread 1.2 Runnable1.3 Callable 2. thread status2.1 Five statuses: creation, ready, running, blocking, and death 2.2 Terminating thread: natural termination, external interference2.3 blocking: Join and yield, Sleep

Difference between sleep method and wait method:The two subordinate classes are different.
Sleep: sleep does not release the lock from the Thread class.
Wait: the lock to be released belongs to the Object class.

3. Thread Information

Thread. currentThread ()Current thread, get name, Set Name, set priority, determine status

4. Thread Synchronization(For the same resource)

Why use synchronization?
The access to the same resource by multiple threads is insecure. To ensure resource accuracy and security, we need to add synchronization.

4.1 Synchronization Method

Modifier synchronized Method Name ()
{
Method body
}

4.1 synchronization Block

Synchronized (reference type | this | class. class ){
}

Note:Too many syncs may cause deadlocks.

V. producer and consumer model Vi. Task Scheduling

Timer, ScheduledExecutor, etc.

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.