???????????·???????Java Concurrency in Practice?????????????
?????????Java????????????????????????:
????* ??????????
????* ??????
????* ???????
????????????????Java????????????????????????????????????????
?????Щ????
????Amdahl????

?????????N??????????????У???????????Sppedup<=1/(F+(1-F)/n)????N?????????????????????1/F?????????????????50%??????????????У??????????????2?????????????10%??????????????У?????????????10??
????Happens-Before
????JMM??????????е?????????????????????????Happens-Before??????????в???B????????????????A??????????A??B?? ???????????????У????????A??B??????????Happens-Before??????????????????????Happens-Before???????? JVM????????????????????????2???μ????н????
????Happens-Before??????????
?????????????? ?????????A?????B????????????A????????B????????С?????????У?
???????????????? ???????????????????????????????????????????????????С?
????volatile???????? ??volatile??????д???????????????????????????С?
?????????????? ????????Thread.Start????????????????????κβ???????С?
?????????????? ????е??κβ????????????????????????????????????У??????Thread.join?г????????????????Thread.isAlive?????false??
?????ж???? ???????????????????????interrupt???????????ж??????interrupt????????У???????InterruptedException?????????isInterrupted??interrupted????
????????????? ???????????????????????????????????????ɡ?
?????????? ???????A?????B????У????????B?????C????У????????A?????????C????С?
???????????Happens??Before????????????????????????????????????????Happens-Before???????JVM?????????????????????????????????????????B??????е????A????????н????B???????????????????????????????????????????????????????е????????????????????????н??????????????????
????final?????????
????????????????????????????????????????????????????????????????final?????????????????????ú??????????????? ??????????????????????????????final?????????????????????final??????????????????final???????HashMap??????? ???????????????????????
???????????final????????????????????????????????????????????????????????????????????????final???????д??? ??????????????Щ??????????κα?????д?????????????“????”???????κλ?????????????????????????????????????????????final??? ?????????????д??????????????????????????????????
??????????????????????final?????????????????????????????????????final?????????????????????????п??????????????????????????????
???????????
????????

????????????????????????????????await()??????????countDown()????????????????????0??
1     /**
2      * ????
3      */
4     public long countDownLatch() throws InterruptedException {
5         int nThreads = 5;
6         final CountDownLatch startGate = new CountDownLatch(1);
7         final CountDownLatch endGate = new CountDownLatch(nThreads);
8
9         for (int i = 0; i < nThreads; i++) {
10             Thread t = new Thread() {
11                 public void run() {
12                     try {
13                         startGate.await();
14                         sleep(1000);
15                     } catch (InterruptedException e) {
16                         e.printStackTrace();
17                     }
18                     endGate.countDown();
19                 }
20             };
21             t.start();
22         }
23
24         long start = System.nanoTime();
25         startGate.countDown();
26         endGate.await();
27         long end = System.nanoTime();
28         return end - start;
29     }
????FutureTask
?????????????????????л??????????????get()????????????????????????????????????????????????????????????????????
1     /**
2      * FutureTask
3      */
4     public String futureTask() throws ExecutionException?? InterruptedException {
5         FutureTask<String> future = new FutureTask<>(() -> {
6             Thread.currentThread().sleep(3000);
7             return "Hello Future.";
8         });
9
10         Thread thread = new Thread(future);
11         thread.start();
12         System.out.println("future.get()");
13         return future.get();
14     }