mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-07-03 10:46:45 +08:00
multi-thread/2020最新Java并发进阶常见面试题总结 排版修正
This commit is contained in:
parent
e646892384
commit
8d9d394270
@ -381,8 +381,7 @@ Thread Name= 9 formatter = yy-M-d ah:mm
|
||||
```java
|
||||
private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
|
||||
@Override
|
||||
protected SimpleDateFormat initialValue()
|
||||
{
|
||||
protected SimpleDateFormat initialValue(){
|
||||
return new SimpleDateFormat("yyyyMMdd HHmm");
|
||||
}
|
||||
};
|
||||
@ -394,13 +393,13 @@ Thread Name= 9 formatter = yy-M-d ah:mm
|
||||
|
||||
```java
|
||||
public class Thread implements Runnable {
|
||||
......
|
||||
//......
|
||||
//与此线程有关的ThreadLocal值。由ThreadLocal类维护
|
||||
ThreadLocal.ThreadLocalMap threadLocals = null;
|
||||
|
||||
//与此线程有关的InheritableThreadLocal值。由InheritableThreadLocal类维护
|
||||
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
|
||||
......
|
||||
//......
|
||||
}
|
||||
```
|
||||
|
||||
@ -428,7 +427,7 @@ ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
|
||||
|
||||
```java
|
||||
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
|
||||
......
|
||||
//......
|
||||
}
|
||||
```
|
||||
|
||||
@ -478,8 +477,7 @@ ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
|
||||
|
||||
### 4.2. 实现 Runnable 接口和 Callable 接口的区别
|
||||
|
||||
`Runnable`自 Java 1.0 以来一直存在,但`Callable`仅在 Java 1.5
|
||||
中引入,目的就是为了来处理`Runnable`不支持的用例。`Runnable`**接口不会返回结果或抛出检查异常,但是**`Callable`**接口可以。所以,如果任务不需要返回结果或抛出异常推荐使用**`Runnable`**接口**,这样代码看起来会更加简洁。
|
||||
`Runnable`自 Java 1.0 以来一直存在,但`Callable`仅在 Java 1.5 中引入,目的就是为了来处理`Runnable`不支持的用例。** `Runnable`接口 **不会返回结果或抛出检查异常,但是** `Callable`接口 **可以。所以,如果任务不需要返回结果或抛出异常推荐使用** `Runnable`接口 **,这样代码看起来会更加简洁。
|
||||
|
||||
工具类 `Executors` 可以实现 `Runnable` 对象和 `Callable` 对象之间的相互转换。(`Executors.callable(Runnable task`)或 `Executors.callable(Runnable task,Object resule)`)。
|
||||
|
||||
@ -713,7 +711,6 @@ public class ThreadPoolExecutorDemo {
|
||||
System.out.println("Finished all threads");
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
可以看到我们上面的代码指定了:
|
||||
@ -1042,7 +1039,7 @@ public class CountDownLatchExample1 {
|
||||
threadPool.execute(() -> {
|
||||
try {
|
||||
//处理文件的业务操作
|
||||
......
|
||||
//......
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
@ -1056,7 +1053,6 @@ public class CountDownLatchExample1 {
|
||||
threadPool.shutdown();
|
||||
System.out.println("finish");
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
@ -1080,7 +1076,7 @@ CompletableFuture<Void> task6 =
|
||||
try {
|
||||
headerFuture.join();
|
||||
} catch (Exception ex) {
|
||||
......
|
||||
//......
|
||||
}
|
||||
System.out.println("all done. ");
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user