"Art Java concurrent programming" study notes (a)

Then a series, is about "Java Concurrency in the art of" reading the book notes and related knowledge, mainly in order to facilitate future review times and prevent forgetting. Ado, directly into the topic:

Chapter 1 concurrent programming challenges

 Concurrent programming goal is to make programs run faster, but not to start more threads can make the program to maximize concurrent execution. Concurrent programming will face many challenges, such as: context switching problem, deadlock, and the resources limited by hardware and software constraints.

1.1 context switching

       Before context switch will save the state of a task, in order to switch back to the next task, you can then load into this state. Context switch from one task is to save the process of reloading.

    1, multi-threaded certain fast?

            Not necessarily. Because the creation of threads and context switching overhead will take some time.

    2, how to reduce context switching

            Method: No concurrent programming lock, CAS algorithm, with minimal use of threads and coroutine.

1.2 Deadlock

 Look at a simple scenario:

 1 public class DeadLockDemo {
 2 private static String A = "A";
 3 private static String B = "B";
 4 public static void main(String[] args) {
 5 new DeadLockDemo().deadLock();
 6 }
 7 private void deadLock() {
 8 Thread t1 = new Thread(new Runnable() {
 9 @Override
10 publicvoid run() {
11 synchronized (A) {
12 try { Thread.currentThread().sleep(2000);
13 } catch (InterruptedException e) {
14 e.printStackTrace();
15 }
16 synchronized (B) {
17 System.out.println("1");
18 }
19 }
20 }
21 });
22 Thread t2 = new Thread(new Runnable() {
23 @Override
24 publicvoid run() {
25 synchronized (B) {
26 synchronized (A) {
27 System.out.println("2");
28 }
29 }
30 }
31 });
32 t1.start();
33 t2.start();
34 }
35 }
View Code

 This code will cause a deadlock, the thread t1 and t2 threads waiting for each other to release the lock.

    Common methods to avoid deadlock:

        1) obtaining a plurality of threads to avoid a lock;

        2) Avoid a thread simultaneously occupy multiple resources in the lock, try to ensure that each lock only takes one resource;

        3) Try to use the time lock, using lock.tryLock (timeout) instead of using the internal lock

        4) for database locks, locking and unlocking must be a database connection, otherwise it will fail to unlock the situation.

1.3 resource constraints challenge

        Resource limitations: Refers during concurrent programming, program execution speed is limited by the computer hardware resources or software resources.

  Solve the problem of resource constraints:

      1) For hardware resource constraints, consider using a cluster of parallel execution of the program. Since stand-alone resource is limited, then let the program run on multiple machines;

      2) For software resource constraints, consider using a resource pool resource reuse.

1.4 Summary

  More use of contract JDK and concurrent containers and provide tools to solve concurrency problems, because these classes have passed fully tested and optimized, can solve the above challenges


Guess you like

Origin www.cnblogs.com/mYunYu/p/12421546.html