From b9726e79f31744e07f45287b63b8803d49809557 Mon Sep 17 00:00:00 2001 From: lazyli Date: Sun, 21 Feb 2016 23:06:41 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E7=9A=84=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E7=9A=84=E9=A1=BA=E5=BA=8F=E7=94=B1runnable=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=9A=84=E9=A1=BA=E5=BA=8F=E5=86=B3=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java8/samples/concurrent/Threads1.java | 49 +++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/com/winterbe/java8/samples/concurrent/Threads1.java b/src/com/winterbe/java8/samples/concurrent/Threads1.java index 8a64f688..31eff9a0 100644 --- a/src/com/winterbe/java8/samples/concurrent/Threads1.java +++ b/src/com/winterbe/java8/samples/concurrent/Threads1.java @@ -8,8 +8,8 @@ public class Threads1 { public static void main(String[] args) { - test1(); -// test2(); +// test1(); + test2(); // test3(); } @@ -32,20 +32,55 @@ private static void test3() { private static void test2() { Runnable runnable = () -> { try { - System.out.println("Foo " + Thread.currentThread().getName()); + System.out.println("begin " + Thread.currentThread().getName()); Thread.sleep(1000); - System.out.println("Bar " + Thread.currentThread().getName()); + System.out.println("end " + Thread.currentThread().getName()); } catch (InterruptedException e) { e.printStackTrace(); } }; + + Runnable r2 = () -> { + try { + System.out.println("begin " + Thread.currentThread().getName()); + Thread.sleep(500); + System.out.println("end " + Thread.currentThread().getName()); + } + catch (InterruptedException e) { + e.printStackTrace(); + } + }; + + + Thread thread = new Thread(runnable); thread.start(); + + + Thread thread2 = new Thread(runnable); + thread2.start(); +// 输出结果随机的。1000秒结束后,谁先被唤醒谁执行 +// begin Thread-0 +// begin Thread-1 +// end Thread-1 +// end Thread-0 +// +// begin Thread-0 +// begin Thread-1 +// end Thread-0 +// end Thread-1 + + } private static void test1() { + Runnable r2 = () -> { + String threadName = Thread.currentThread().getName(); + System.out.println("你好 " + threadName); + }; //线程的执行的顺序由runnable定义的顺序决定 + Runnable runnable = () -> { String threadName = Thread.currentThread().getName(); System.out.println("Hello " + threadName); @@ -55,7 +90,13 @@ private static void test1() { Thread thread = new Thread(runnable); thread.start(); +// thread.start(); //第二次startu + + Thread thread2 = new Thread(r2); + thread2.start(); + System.out.println("Done!"); } } + From 0db38fafedf37284d15fa7b4faf6edb9050db0f9 Mon Sep 17 00:00:00 2001 From: lazyli Date: Sun, 21 Feb 2016 23:09:21 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=B3=A8=E6=84=8Fexecutor.shutdown?= =?UTF-8?q?=E4=B8=8D=E6=98=AF=E9=A9=AC=E4=B8=8A=E7=BB=93=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 妯″潡鎵ц椤哄簭 --- .../java8/samples/concurrent/Executors1.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/com/winterbe/java8/samples/concurrent/Executors1.java b/src/com/winterbe/java8/samples/concurrent/Executors1.java index 9762938d..e8b3a828 100644 --- a/src/com/winterbe/java8/samples/concurrent/Executors1.java +++ b/src/com/winterbe/java8/samples/concurrent/Executors1.java @@ -10,7 +10,7 @@ public class Executors1 { public static void main(String[] args) { - test1(3); + test1(2); // test1(7); } @@ -32,17 +32,30 @@ private static void test1(long seconds) { static void stop(ExecutorService executor) { try { System.out.println("attempt to shutdown executor"); - executor.shutdown(); - executor.awaitTermination(5, TimeUnit.SECONDS); - } - catch (InterruptedException e) { - System.err.println("termination interrupted"); + executor.shutdown(); //waits for currently running tasks to finish 没有马上结束 + System.out.println("waits for currently running tasks to finish"); + +// try { +// Thread.sleep(2500); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } + + //executor.awaitTermination(5, TimeUnit.SECONDS); //等待 + + System.out.println("waiting finish"); + + } - finally { +// catch (InterruptedException e) { +// System.err.println("termination interrupted"); +// } + finally + { if (!executor.isTerminated()) { System.err.println("killing non-finished tasks"); } - executor.shutdownNow(); + executor.shutdownNow(); //立即结束 System.out.println("shutdown finished"); } } From 1b5fa65618ed21c1a203bccf5c9bdb51b5ab3bbe Mon Sep 17 00:00:00 2001 From: lazyli Date: Mon, 22 Feb 2016 23:00:22 +0800 Subject: [PATCH 3/3] Executor tests --- .../java8/samples/concurrent/Executors2.java | 21 ++++++++---- .../java8/samples/concurrent/Executors3.java | 32 +++++++++++++------ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/com/winterbe/java8/samples/concurrent/Executors2.java b/src/com/winterbe/java8/samples/concurrent/Executors2.java index 33dda88e..3a355ece 100644 --- a/src/com/winterbe/java8/samples/concurrent/Executors2.java +++ b/src/com/winterbe/java8/samples/concurrent/Executors2.java @@ -14,8 +14,8 @@ public class Executors2 { public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException { // test1(); -// test2(); - test3(); + test2(); +// test3(); } private static void test3() throws InterruptedException, ExecutionException, TimeoutException { @@ -66,11 +66,18 @@ private static void test1() throws InterruptedException, ExecutionException { System.out.println("future done: " + future.isDone()); - Integer result = future.get(); - - System.out.println("future done: " + future.isDone()); - System.out.print("result: " + result); - + Integer result; + try { + result = future.get(2, TimeUnit.SECONDS); + + + System.out.println("future done: " + future.isDone()); + System.out.print("result: " + result); + + } catch (TimeoutException e) { + System.out.println("future.get time too short "); //future.get 时间必须等待submit结束后 + //e.printStackTrace(); + } executor.shutdownNow(); } diff --git a/src/com/winterbe/java8/samples/concurrent/Executors3.java b/src/com/winterbe/java8/samples/concurrent/Executors3.java index 75e60377..232be78b 100644 --- a/src/com/winterbe/java8/samples/concurrent/Executors3.java +++ b/src/com/winterbe/java8/samples/concurrent/Executors3.java @@ -6,6 +6,7 @@ import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -18,9 +19,8 @@ public class Executors3 { public static void main(String[] args) throws InterruptedException, ExecutionException { test1(); // test2(); -// test3(); - -// test4(); +// test3(); +// test4(); // test5(); } @@ -32,8 +32,12 @@ private static void test5() throws InterruptedException, ExecutionException { callable("task2", 1), callable("task3", 3)); - String result = executor.invokeAny(callables); - System.out.println(result); + String result = executor.invokeAny(callables); // invoke any executor shutdown之前可以重复invoke + System.out.println(result); + + List> resultA = executor.invokeAll(callables); //invoke all 需要使用future类型来取的返回值 + for(Future f:resultA) + System.out.println("asdf:" +f.get().toString()); executor.shutdown(); } @@ -41,7 +45,7 @@ private static void test5() throws InterruptedException, ExecutionException { private static Callable callable(String result, long sleepSeconds) { return () -> { TimeUnit.SECONDS.sleep(sleepSeconds); - return result; + return result +": " +sleepSeconds; }; } @@ -53,9 +57,7 @@ private static void test4() throws InterruptedException { () -> "task2", () -> "task3"); - executor.invokeAll(callables) - .stream() - .map(future -> { + executor.invokeAll(callables).stream().map(future -> { try { return future.get(); } @@ -68,7 +70,7 @@ private static void test4() throws InterruptedException { executor.shutdown(); } - private static void test3() { + private static void test3() throws InterruptedException { ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); Runnable task = () -> { @@ -82,6 +84,16 @@ private static void test3() { }; executor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS); + executor.awaitTermination(4, TimeUnit.SECONDS); //当前线程停止4秒,scheduling仍执行2次后,输出 end + + // Scheduling: 267468909984462 + // Scheduling: 267470912146948 + // end + // Scheduling: 267472914815118 + System.out.println("end"); +// executor.shutdown(); //执行一次结束 +// executor.shutdownNow(); //马上结束 + } private static void test2() {