Threads and Thread Pools

Source: Internet
Author: User

The first thread has both a daemon thread and a user thread, the difference being whether the user thread keeps the program running. When the program is running, there must be one or more threads that are user threads, and when the program finishes, all the daemon threads will be closed. Using Thread.setdaemon (ture), you can mark a thread as a daemon thread, and the default thread state inherits from the thread that created it. Two ways to create a thread are not much to say.

Thread safety generally refers to the inconsistent data or data integrity of shared variables that are accessed and read by multiple threads. Generally there are several methods to be consulted:

The 1.synchronized method provides a class, method, or block of statements that can be accessed only by one thread, and the modification of the control variable is within the controllable range.

The 2.lock method can be controlled using a read-write or a re-entry lock within the Java.util.concurrent.locks package.

3.ThreadLocal variable to get a copy of a variable for each thread separately.

4. Atomic operations at the machine instruction level, such as variables in the Java.util.concurrent.atomic package. Use CAs (compare and swap) to ensure the validity of data updates. That is, the lock-free mechanism to ensure high concurrency performance, maintain high throughput.

5. I'm not sure yet.

The JDK itself provides a thread pool implementation that is convenient to use.

There are so many thread pools in general

Cond

Threads and Thread Pools

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.