Some basic knowledge of Java threading

Source: Internet
Author: User


--------------------------------------------------------------------------------------------------
The life cycle of a thread:
New, ready, execute, block, perish

With the new keyword, the thread is created to enter the new State;
When a thread calls the start () method, the thread enters a ready state, meaning that the thread has the right to acquire the time slice of the cup and wait for the JVM to dispatch;
When the thread acquires the cup, the thread is executed, which means that the thread's run () method is executed;
When the thread's run () method does not complete, but loses the cup time slice, at this time the thread enters the blocking state, waits for the next time to be dispatched;
When the thread's run () method executes the settlement, the thread dies;

Multithreading does not improve the execution efficiency of the program, but increases the use of the program by the Cup load probability, multithreading is not parallel execution;
--------------------------------------------------------------------------------------------------
Multithreading: Refers to the program (a process) when the runtime produces more than one thread
Parallel and Concurrency:
Parallel: Multiple CPU instances or multiple machines executing a processing logic at the same time are true at the same time.
Concurrency: Through the CPU scheduling algorithm, let the user appear to execute simultaneously, actually from the CPU operation level is not real at the same time. Concurrency tends to be common in the scene
Resources, the bottleneck is often created for this common resource, and we use TPS or QPS to respond to the system's processing power.
--------------------------------------------------------------------------------------------------
Scheduling and control of threads:
The scheduling model of the thread is divided into: time-sharing scheduling model and preemptive scheduling model, Java using preemptive scheduling model
-
Time-sharing scheduling model: All threads take turns using the CPU's right of use, evenly allocating each thread to occupy the CPU of the chip
Preemptive scheduling model: Priority for high-priority threads to use the CPU, if the priority of the thread is the same, then randomly select a high-priority thread gets more CPU time slices.
--------------------------------------------------------------------------------------------------
How to avoid deadlocks:
Package the same resources, potentially generating deadlocks, into a group and put them in a public lock!
--------------------------------------------------------------------------------------------------

Some basic knowledge of Java threading

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.