Java High concurrency Programming (i)

Source: Internet
Author: User

1. Atomic weight operation (read, + + operation, write into the smallest unit of operation, in multi-threaded atomic weight programming to ensure program visibility (ordered))

Because of some problems in multithreaded conditions: there is a problem of competition, (for example: a simple counter in a multi-threaded increase) If the program does not adopt the mechanism of synchronization, then in the program's running results, multiple threads when accessing this resource, generate racing. Solve this problem by blocking other threads from using the variable when it is used by the thread in some way

Atomic-level operation: 1. Locking mechanism (best operation) 2.java.concurrent.atomic package contains some atomic weight operations: Atomiclong class, although the atomic weight in the atomic package operation, in these simple can guarantee the correctness of the results run, but also has its defective type:

I. When multi-threading, if a state is added from a stateless class, the class is managed by the thread's security object and is evaluated using the atomic package (called thread-safe mode) to get a good result

II. When the state is increased from one to multiple states, and the variables are not independent of each other, have a certain connection, and access to a resource at the same time, do not use the atomic class, the concept of locking

2.Java High concurrency Programming: (Just simple knowledge of understanding, jdk1.5 new version of atomic weight, thread safety) in image processing and server-side (data mining) programming, CPU frequency 4GHZ

I.CPU Parallel (Network programming, server-side use), HTTP server

1. Synchronous and asynchronous: a synchronous call to a procedure call will wait for a response to return (synchronized) before it has been seen in the synchronization mechanism, asynchronous refers to directly after the call to return, in the execution of other things in other threads

1. Parallel Vs Concurrency: A single CPU will not be able to parallel (concurrency), multiple CPUs may run in parallel

2. Critical section:

3. Blocking vs non-blocking

Congestion cost: Spend more than 80,000 clocks on something (there is a willing to deadlock situation)

Deadlock: (blocking) is equivalent to static problem, CPU occupancy rate is 0;

Live Lock: (Elevator meet people, two people constantly face), CPU occupancy rate, resource multiple line threads unlocked and occupy

Starvation: The priority of itself is too low, the data competition is used by high priority threads, they cannot get

Blocking is a pessimistic operation in which a resource can only be occupied by one person (entering a critical section), non-blocking: a positive operation, adhering to the wide-entry strict.

No lock must be barrier-free, no lock in the competition has a thread to win, no wait must not lock

5.2 Laws of parallelism:

In the program F is very large, plus the number of CPUs is not very large

Guustafston law: Only when the F-serial ratio is small, can high performance

To sum up: only adjust the number of jargon in the program, while processing CPU to make the program perform better

Java High 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.