提交 3c55b40f 编写于 作者: M Mars Liu

future

上级 afffaf21
{
"node_id": "java-b45c34278b714bfbb6a6d80817e24639",
"keywords": [],
"children": [],
"export": [],
"title": "什么是Java"
}
\ No newline at end of file
{
"node_id": "java-238f8a0558a949f29e56dd830eae1b8b",
"keywords": [],
"children": [],
"export": [],
"title": "JDK的下载"
}
\ No newline at end of file
{
"node_id": "java-007e13f0e4db49a1b181b1561104873c",
"keywords": [],
"children": [],
"export": [],
"title": "配置JDK"
}
\ No newline at end of file
{
"node_id": "java-32823e681af949c4ae26e0bf9a9977a2",
"keywords": [],
"children": [],
"export": [],
"title": "Java的版本"
}
\ No newline at end of file
{
"node_id": "java-36e8a46b02764541acd2800e1b189403",
"keywords": [],
"children": [],
"export": [],
"title": "JavaAPI文档"
}
\ No newline at end of file
{
"node_id": "java-6d12bdbdeb7144aeb3649d50e0f06007",
"keywords": [],
"children": [],
"export": [],
"title": "测试开发环境"
}
\ No newline at end of file
{
"node_id": "java-43e7bb3ee96c44e29df8e2f4063be49a",
"keywords": [],
"title": "Java概述"
}
\ No newline at end of file
......@@ -2,5 +2,5 @@
"node_id": "java-5dcc74ab2617434f91ae6aa1ba0ac661",
"keywords": [],
"children": [],
"export": []
"export": ["thread.json"]
}
\ No newline at end of file
# 计数器
我们需要在工作线程中统计一个计数,这个计数是全局可见的,下面哪段代码有错?
## 答案
```java
public class Worker extends Thread {
private static int count = 0;
public static int getCount() {
return count;
}
public static void increment() {
count = count + 1;
}
@Override
public void run() {
//... do something
increment();
}
}
```
## 选项
### 内置并发对象
```java
import java.util.concurrent.atomic.AtomicInteger;
public class Worker extends Thread {
public static final AtomicInteger counter = new AtomicInteger(0);
@Override
public void run() {
//... do something
counter.incrementAndGet();
}
}
```
### 朴素的加锁方法
```java
public class Worker extends Thread {
private static volatile int count = 0;
public static int getCount() {
return count;
}
public static synchronized void increment() {
count = count + 1;
}
@Override
public void run() {
//... do something
increment();
}
}
```
### 朴素的锁机制
```java
public class Worker extends Thread {
private static Lock lock = new ReentrantLock();
private static Integer count = 0;
public static int getCount() {
return count;
}
public static void increment() {
lock.lock();
try {
count = count + 1;
} finally {
lock.unlock();
}
}
@Override
public void run() {
//... do something
increment();
}
}
```
......@@ -2,5 +2,6 @@
"type": "code_options",
"author": "刘鑫",
"source": "thread.md",
"notebook_enable": false
"notebook_enable": false,
"exercise_id": "c1b604a5d2364a969361cd5fbb4ed3f8"
}
\ No newline at end of file
# 线程
关于线程,哪一个选项的叙述是错的?
## 答案
线程比进程快。
## 选项
### A
Java 的多线程代码可以并行在多个CPU上
### B
同一进程内的线程可以共享变量,而进程之间不共享变量。
### C
Java 的线程对象 Thread 调用 sleep 时,不阻塞其它线程
### D
通过实现 Thread 类型,可以自定义线程任务。
### E
可以在 Thread 的构造函数中传入一个 Runnable 对象来构造一个线程对象。
### F
Java 提供了了一些内置线程池类型,基于线程模型的异步任务可以使用这些资源。
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "future.md",
"notebook_enable": false
}
\ No newline at end of file
# Future
我们有如下代码:
```java
import java.util.concurrent.Executors;
ExecutorService pool = Executors.newCachedThreadPool();
var amount = 0.0f;
List<Future<Float>> futures = //submit jobs into the pool for create futures list
for(var f: futures){
amount += f.get();
}
//...
```
下面分析中,错误的是
## 答案
此代码中有并发冲突
## 选项
### A
如果future执行过程中有较多的阻塞或低速逻辑,有可能阻塞所有future。
### B
Future 对象的 get 方法有可能抛出异常。
### C
这段程序的执行时间取决于任务池的并行设置和最慢的future的执行时间。
### D
Future 可以不加修改运行在不同的 ExecutorService 实现上。
\ No newline at end of file
{
"node_id": "java-16a45f5c4e8242519004fe429dd335c9",
"keywords": [],
"children": [],
"export": [],
"title": "“线框–管道”模型"
}
\ No newline at end of file
{
"node_id": "java-af9fa0cd03d643fdb6eecd75272140cf",
"keywords": [],
"children": [
{
"背压": {
"keywords": [],
"children": [],
"node_id": "java-bb8cf6772cb340c1adb74e9b231357a8",
"title": "背压"
}
},
{
"一种简单的真实背压": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "一种简单的真实背压"
}
}
],
"export": [],
"title": "“发布–订阅”以及反应式编程"
}
\ No newline at end of file
{
"node_id": "java-d720f1f3560343559bcc30eca566d317",
"keywords": [],
"children": [],
"export": [],
"title": "反应式系统和反应式编程"
}
\ No newline at end of file
{
"node_id": "java-96551e0eb6b044ff8f7fe9b5ff27fd73",
"keywords": [],
"children": [
{
"Flow类": {
"keywords": [],
"children": [],
"node_id": "java-35293e7729734213bcd67b97853eb50b",
"title": "Flow类"
}
},
{
"创建你的第一个反应式应用": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "创建你的第一个反应式应用"
}
},
{
"使用Processor转换数据": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "使用Processor转换数据"
}
}
],
"export": [],
"title": "反应式流以及Flow API"
}
\ No newline at end of file
{
"node_id": "java-bf773ed23aad4739866b582a3679e381",
"keywords": [],
"children": [
{
"转换及整合多个Observable": {
"keywords": [],
"children": [],
"node_id": "java-5a62f6f76fc04129aa628b3bd0f22609",
"title": "转换及整合多个Observable"
}
}
],
"export": [],
"title": "使用反应式库RxJava"
}
\ No newline at end of file
{
"node_id": "java-313674f323ec43919376c21a7e1bc88b",
"keywords": [],
"children": [
{
"通过反射构建对象": {
"keywords": [],
"children": [],
"node_id": "java-ac54833922254dceaccce5a2ffb0bf08",
"title": "通过反射构建对象"
}
},
{
"反射方法": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "反射方法"
}
}
],
"export": [],
"title": "Java反射技术"
}
\ No newline at end of file
{
"node_id": "java-0969aecf77a7416ba0fed596b2569a23",
"keywords": [],
"children": [
{
"对最佳价格查询器应用的优化": {
"keywords": [],
"children": [],
"node_id": "java-39915bb35ba34d41a11b6715aa7c687b",
"title": "对最佳价格查询器应用的优化"
}
}
],
"export": [],
"title": "反应式编程"
}
\ No newline at end of file
{
"node_id": "java-e5d307b8ce2049648b4afd9fecdb48b0",
"keywords": []
}
\ No newline at end of file
......@@ -8,56 +8,6 @@
"node_id": "java-71a99d2763734420aee110c5c5b904d8",
"keywords": [],
"children": [
{
"Java概述": {
"node_id": "java-43e7bb3ee96c44e29df8e2f4063be49a",
"keywords": [],
"children": [
{
"什么是Java": {
"node_id": "java-b45c34278b714bfbb6a6d80817e24639",
"keywords": [],
"children": []
}
},
{
"JDK的下载": {
"node_id": "java-238f8a0558a949f29e56dd830eae1b8b",
"keywords": [],
"children": []
}
},
{
"配置JDK": {
"node_id": "java-007e13f0e4db49a1b181b1561104873c",
"keywords": [],
"children": []
}
},
{
"Java的版本": {
"node_id": "java-32823e681af949c4ae26e0bf9a9977a2",
"keywords": [],
"children": []
}
},
{
"JavaAPI文档": {
"node_id": "java-36e8a46b02764541acd2800e1b189403",
"keywords": [],
"children": []
}
},
{
"测试开发环境": {
"node_id": "java-6d12bdbdeb7144aeb3649d50e0f06007",
"keywords": [],
"children": []
}
}
]
}
},
{
"第一个Java程序": {
"node_id": "java-379b792dde024090a7ed43b392ece2c5",
......@@ -4038,139 +3988,14 @@
}
},
{
"“线框–管道”模型": {
"node_id": "java-16a45f5c4e8242519004fe429dd335c9",
"keywords": [],
"children": []
}
},
{
"为并发而生的CompletableFuture和结合器": {
"CompletableFuture": {
"node_id": "java-90f4b7c696464886af9c8de8cbd95338",
"keywords": [],
"children": []
}
},
{
"“发布–订阅”以及反应式编程": {
"node_id": "java-af9fa0cd03d643fdb6eecd75272140cf",
"keywords": [],
"children": [
{
"背压": {
"keywords": [],
"children": [],
"node_id": "java-bb8cf6772cb340c1adb74e9b231357a8",
"title": "背压"
}
},
{
"一种简单的真实背压": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "一种简单的真实背压"
}
}
]
}
},
{
"反应式系统和反应式编程": {
"node_id": "java-d720f1f3560343559bcc30eca566d317",
"keywords": [],
"children": []
}
}
]
}
},
{
"反应式编程": {
"node_id": "java-0969aecf77a7416ba0fed596b2569a23",
"keywords": [],
"children": [
{
"反应式流以及Flow API": {
"node_id": "java-96551e0eb6b044ff8f7fe9b5ff27fd73",
"keywords": [],
"children": [
{
"Flow类": {
"keywords": [],
"children": [],
"node_id": "java-35293e7729734213bcd67b97853eb50b",
"title": "Flow类"
}
},
{
"创建你的第一个反应式应用": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "创建你的第一个反应式应用"
}
},
{
"使用Processor转换数据": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "使用Processor转换数据"
}
}
]
}
},
{
"使用反应式库RxJava": {
"node_id": "java-bf773ed23aad4739866b582a3679e381",
"keywords": [],
"children": [
{
"转换及整合多个Observable": {
"keywords": [],
"children": [],
"node_id": "java-5a62f6f76fc04129aa628b3bd0f22609",
"title": "转换及整合多个Observable"
}
}
]
}
},
{
"Java反射技术": {
"node_id": "java-313674f323ec43919376c21a7e1bc88b",
"keywords": [],
"children": [
{
"通过反射构建对象": {
"keywords": [],
"children": [],
"node_id": "java-ac54833922254dceaccce5a2ffb0bf08",
"title": "通过反射构建对象"
}
},
{
"反射方法": {
"keywords": [],
"children": [],
"node_id": "569d5e11c4fc5de7844053d9a733c5e8",
"title": "反射方法"
}
}
]
}
}
]
}
},
{
"Apache Common": {
"node_id": "java-e5d307b8ce2049648b4afd9fecdb48b0",
"keywords": [],
"children": []
}
}
]
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册