forked from Snailclimb/JavaGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bfa563
commit 87009c4
Showing
15 changed files
with
515 additions
and
164 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file removed
BIN
-1.92 KB
...ThreadPoolExecutorDemo/out/production/ThreadPoolExecutorDemo/ThreadPoolExecutorDemo.class
Binary file not shown.
Binary file removed
BIN
-1.34 KB
code/java/ThreadPoolExecutorDemo/out/production/ThreadPoolExecutorDemo/WorkerThread.class
Binary file not shown.
Binary file added
BIN
+2.76 KB
.../ThreadPoolExecutorDemo/out/production/ThreadPoolExecutorDemo/callable/CallableDemo.class
Binary file not shown.
Binary file added
BIN
+782 Bytes
...va/ThreadPoolExecutorDemo/out/production/ThreadPoolExecutorDemo/callable/MyCallable.class
Binary file not shown.
Binary file added
BIN
+607 Bytes
...adPoolExecutorDemo/out/production/ThreadPoolExecutorDemo/common/ThreadPoolConstants.class
Binary file not shown.
Binary file added
BIN
+1.78 KB
...emo/out/production/ThreadPoolExecutorDemo/threadPoolExecutor/ThreadPoolExecutorDemo.class
Binary file not shown.
Binary file added
BIN
+1.38 KB
...lExecutorDemo/out/production/ThreadPoolExecutorDemo/threadPoolExecutor/WorkerThread.class
Binary file not shown.
49 changes: 49 additions & 0 deletions
49
code/java/ThreadPoolExecutorDemo/src/callable/CallableDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package callable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ThreadPoolExecutor; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static common.ThreadPoolConstants.CORE_POOL_SIZE; | ||
import static common.ThreadPoolConstants.KEEP_ALIVE_TIME; | ||
import static common.ThreadPoolConstants.MAX_POOL_SIZE; | ||
import static common.ThreadPoolConstants.QUEUE_CAPACITY; | ||
|
||
public class CallableDemo { | ||
public static void main(String[] args) { | ||
//使用阿里巴巴推荐的创建线程池的方式 | ||
//通过ThreadPoolExecutor构造函数自定义参数创建 | ||
ThreadPoolExecutor executor = new ThreadPoolExecutor( | ||
CORE_POOL_SIZE, | ||
MAX_POOL_SIZE, | ||
KEEP_ALIVE_TIME, | ||
TimeUnit.SECONDS, | ||
new ArrayBlockingQueue<>(QUEUE_CAPACITY), | ||
new ThreadPoolExecutor.CallerRunsPolicy()); | ||
|
||
List<Future<String>> futureList = new ArrayList<>(); | ||
Callable<String> callable = new MyCallable(); | ||
for (int i = 0; i < 10; i++) { | ||
//提交任务到线程池 | ||
Future<String> future = executor.submit(callable); | ||
//将返回值 future 添加到 list,我们可以通过 future 获得 执行 Callable 得到的返回值 | ||
futureList.add(future); | ||
} | ||
for (Future<String> fut : futureList) { | ||
try { | ||
System.out.println(new Date() + "::" + fut.get()); | ||
} catch (InterruptedException | ExecutionException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
//关闭线程池 | ||
executor.shutdown(); | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
code/java/ThreadPoolExecutorDemo/src/callable/MyCallable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package callable; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
public class MyCallable implements Callable<String> { | ||
|
||
@Override | ||
public String call() throws Exception { | ||
Thread.sleep(1000); | ||
//返回执行当前 Callable 的线程名字 | ||
return Thread.currentThread().getName(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
code/java/ThreadPoolExecutorDemo/src/common/ThreadPoolConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package common; | ||
|
||
public class ThreadPoolConstants { | ||
public static final int CORE_POOL_SIZE = 5; | ||
public static final int MAX_POOL_SIZE = 10; | ||
public static final int QUEUE_CAPACITY = 100; | ||
public static final Long KEEP_ALIVE_TIME = 1L; | ||
private ThreadPoolConstants(){ | ||
|
||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
...eadPoolExecutorDemo/src/WorkerThread.java → ...mo/src/threadPoolExecutor/MyRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.