Java????????????
?????Katsura ???????[ 2016/9/1 11:38:21 ] ??????????????? Java
???????????·???????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 }
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11