Getting Started with Java Concurrency Programming (i)

Source: Internet
Author: User

First, why should concurrency?

Background: The presence of the operating system makes it possible for the computer to run multiple programs at the same time.

1. Purpose:

Resource utilization. At some point, the program has to wait for some external operations to complete (IO) to continue running, and the wait time to run other programs can effectively improve resource utilization.

Fairness. Different users and programs have fair utilization of the computer's resources.

Ease of access. In order to complete a task, it is more convenient to run multiple computer programs and communicate at the same time than to run only one computer program.

2. Threads

Threads are also known as lightweight processes, and if there is no scheduling mechanism, the threads will run independently. All threads in the same process share the address space of the process, enabling a finer-grained resource-sharing mechanism.

Benefits : Reduce program development and maintenance costs, and improve the performance of complex applications. Threads can transform most asynchronous workflows into serial workflows, which can better simulate how humans work and interact, and improve machine resource utilization and throughput.

The benefits of performing the same type of tasks: Reduce switching costs, program easier to write and test, and reduce error rates.

Risk :

Security: In the absence of sufficient synchronization, the order of execution of multiple threads is unpredictable and may even have strange consequences.

Active: A potential blocking problem.

Performance: The additional overhead associated with thread scheduling when multiple threads are introduced.

Second, thread safety

1 , Core: Management of State access operations, especially for shared ( Shared ) and the variable ( Mutable ) State of access.

"Sharing" means that variables can be accessed concurrently by multiple threads, while "mutable" means that the value of a variable can change over its life cycle. Whether an object is thread-safe depends on whether it is accessed by more than one thread, and to achieve thread safety, a synchronization mechanism is required to coordinate access to the mutable state of the object, which can result in data corruption and other results that should not occur if collaboration is not possible.

Java Synchronization mechanism:

Keyword: synchronized

Provides an exclusive lock-in method.

2 , definition: correctness

Meaning: The behavior of a class is exactly the same as its specification. In good specifications, it is common to define the state of objects that are constrained by various invariant conditions, and to define various post-conditions to describe the results of object manipulation-code trustworthiness.

This class is thread-safe when multiple threads access a class, regardless of how the runtime environment is scheduled or how those threads will be executed alternately, and if no additional synchronization or collaboration is required in the keynote code, and the class behaves correctly.

Example 1: Thread-safe servlet

 Public class Implements servlet{  publicvoid== factor (i); Encodeintoresponse (RESP, fact ORS);}}

A stateless servlet is thread-safe.

Most servlets are stateless, which greatly reduces the complexity of implementing servlet thread safety. Thread safety becomes a problem only if the servlet needs to save information when it processes the request.

3. Atomic Nature

Example 2: A non-thread-safe servlet

 Public class Implements Servlet {    privatelong count = 0;      Public Long GetCount () {        return  count;    }      Public void Service (ServletRequest req, Servletresponse resp) {        = extractfromrequest (req);         = factor (i);    }}

Cause of Thread insecurity: count++ is not an atomic operation, it contains three steps: Read-write-Modify. If two threads read a value of 9 at the same time and perform an additional operation separately, the result is a case of a final value deviation of 1.

Getting Started with Java Concurrency Programming (i)

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.