Java multi-thread mode

Source: Internet
Author: User

The following multi-threaded mode is introduced. Here we mainly list the thread modes of Java and their usage methods. For more information, see Java multithreading mode.

 

1. Single Thread Execution-only one person can use this bridge

This mode uses locks to protect key execution areas.

 

2, Immutable -- unchangeable

Immutable class refers to the class whose status does not change after instantiation.

This mode can be used in the following situations: Make sure that the instance status of the class does not change; instances need to be shared and frequently accessed.

 

3. Guarded Suspension -- wait for me to prepare

The main feature of this mode is that it is in the wait thread and expects to receive policy/policyall when the alert condition is set.

 

4, Balking -- Forget it if you don't need it.

The main feature of this mode is "Don't wait ". When the alert conditions are not met, the system immediately exits and enters the next job.

 

5. Producer-Consumer -- let me do it. You can use it.

Producer and consumer models are the most classic multi-thread models. If the producer thread and consumer thread need to work together to "put something in the middle", synchronization needs to "protect things ".

 

6. Read-Write Lock -- Read it if you want to Read it, but you cannot Write it when you Read it.

This mode is suitable when the read task is heavy.

 

7. Thread-Per-Message-the job is handed over to you.

For time-consuming operations, hand over to the new thread.

 

8. Worker Thread -- work when the job comes.

The difference between this mode and Thread-Per-Message mode: it takes a long time to start a Thread, but it is better to reuse a Thread when a new Thread needs to be created frequently to complete the work, ensure resource reuse. This Thread is the Worker Thread.

In addition, one topic of this mode is the service volume (carrying capacity), which balances the number of Worker threads and requests.

 

9. Future -- first give you a Bill of Lading

In this mode, if obtaining a value is a time-consuming operation, you can separate "prepare return value" from "use return value ". First, "Prepare the return value", and then obtain "Use the return value ".

 

10, Two-Phase Termination -- pack toys and go to bed

The two-step thread termination method is described in detail in the blog http://blog.csdn.net/luoxinwu123/article/details/7660625.

 

11, Thread-Specialfic Storage-safe deposit box for each Thread

In this mode, Thread_Local Storage (TLS) is used to keep some state of the thread.

12. Active Object: the Active Object that receives asynchronous messages

Active objects generally have independent threads. They are characterized by: (1) receiving external requests asynchronously; (2) Free scheduling; (3) the actual processing is performed by a single thread; (4) execution results can be returned; (5) Independent threads are available.

This mode has many elements and is suitable for handling large-scale problems.

 

 

 

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.