1
0
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:
2293736867 2021-06-03 18:48:29 +08:00
parent e646892384
commit 8d9d394270

View File

@ -381,8 +381,7 @@ Thread Name= 9 formatter = yy-M-d ah:mm
```java ```java
private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){ private static final ThreadLocal<SimpleDateFormat> formatter = new ThreadLocal<SimpleDateFormat>(){
@Override @Override
protected SimpleDateFormat initialValue() protected SimpleDateFormat initialValue(){
{
return new SimpleDateFormat("yyyyMMdd HHmm"); return new SimpleDateFormat("yyyyMMdd HHmm");
} }
}; };
@ -394,13 +393,13 @@ Thread Name= 9 formatter = yy-M-d ah:mm
```java ```java
public class Thread implements Runnable { public class Thread implements Runnable {
...... //......
//与此线程有关的ThreadLocal值。由ThreadLocal类维护 //与此线程有关的ThreadLocal值。由ThreadLocal类维护
ThreadLocal.ThreadLocalMap threadLocals = null; ThreadLocal.ThreadLocalMap threadLocals = null;
//与此线程有关的InheritableThreadLocal值。由InheritableThreadLocal类维护 //与此线程有关的InheritableThreadLocal值。由InheritableThreadLocal类维护
ThreadLocal.ThreadLocalMap inheritableThreadLocals = null; ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
...... //......
} }
``` ```
@ -428,7 +427,7 @@ ThreadLocal.ThreadLocalMap inheritableThreadLocals = null;
```java ```java
ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) { ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
...... //......
} }
``` ```
@ -478,8 +477,7 @@ ThreadLocalMap(ThreadLocal<?> firstKey, Object firstValue) {
### 4.2. 实现 Runnable 接口和 Callable 接口的区别 ### 4.2. 实现 Runnable 接口和 Callable 接口的区别
`Runnable`自 Java 1.0 以来一直存在,但`Callable`仅在 Java 1.5 `Runnable`自 Java 1.0 以来一直存在,但`Callable`仅在 Java 1.5 中引入,目的就是为了来处理`Runnable`不支持的用例。** `Runnable`接口 **不会返回结果或抛出检查异常,但是** `Callable`接口 **可以。所以,如果任务不需要返回结果或抛出异常推荐使用** `Runnable`接口 **,这样代码看起来会更加简洁。
中引入,目的就是为了来处理`Runnable`不支持的用例。`Runnable`**接口不会返回结果或抛出检查异常,但是**`Callable`**接口可以。所以,如果任务不需要返回结果或抛出异常推荐使用**`Runnable`**接口**,这样代码看起来会更加简洁。
工具类 `Executors` 可以实现 `Runnable` 对象和 `Callable` 对象之间的相互转换。(`Executors.callableRunnable task`)或 `Executors.callableRunnable taskObject resule`)。 工具类 `Executors` 可以实现 `Runnable` 对象和 `Callable` 对象之间的相互转换。(`Executors.callableRunnable task`)或 `Executors.callableRunnable taskObject resule`)。
@ -713,7 +711,6 @@ public class ThreadPoolExecutorDemo {
System.out.println("Finished all threads"); System.out.println("Finished all threads");
} }
} }
``` ```
可以看到我们上面的代码指定了: 可以看到我们上面的代码指定了:
@ -1042,7 +1039,7 @@ public class CountDownLatchExample1 {
threadPool.execute(() -> { threadPool.execute(() -> {
try { try {
//处理文件的业务操作 //处理文件的业务操作
...... //......
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
@ -1056,7 +1053,6 @@ public class CountDownLatchExample1 {
threadPool.shutdown(); threadPool.shutdown();
System.out.println("finish"); System.out.println("finish");
} }
} }
``` ```
@ -1080,7 +1076,7 @@ CompletableFuture<Void> task6 =
try { try {
headerFuture.join(); headerFuture.join();
} catch (Exception ex) { } catch (Exception ex) {
...... //......
} }
System.out.println("all done. "); System.out.println("all done. ");
``` ```