Skip to content

Commit 68178cb

Browse files
author
Beck
committed
note
1 parent 7256707 commit 68178cb

18 files changed

+61
-41
lines changed

src/main/java/com/winterbe/java8/samples/concurrent/AtomicIntegerDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
public class AtomicIntegerDemo {
1212

13-
private static final int NUM_INCREMENTS = 1000;
13+
private static final int NUM_INCREMENTS = 10;
1414

1515
private static AtomicInteger atomicInt = new AtomicInteger(0);
1616

src/main/java/com/winterbe/java8/samples/concurrent/CompletableFuture1.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public class CompletableFuture1 {
1111
public static void main(String[] args) throws ExecutionException, InterruptedException {
1212
CompletableFuture<String> future = new CompletableFuture<>();
1313
future.complete("42");
14-
future.thenAccept(System.out::println).thenAccept(v -> System.out.println("done"));
15-
14+
future.thenAccept(System.out::println).thenAccept(v -> System.out.println("done: "+v));
1615
}
1716
}

src/main/java/com/winterbe/java8/samples/concurrent/LongAccumulator1.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* @author Benjamin Winterberg
11+
* @Message 累加计算
1112
*/
1213
public class LongAccumulator1 {
1314

@@ -19,8 +20,8 @@ private static void testAccumulate() {
1920
LongBinaryOperator op = (x, y) -> 2 * x + y;
2021
LongAccumulator accumulator = new LongAccumulator(op, 1L);
2122
ExecutorService executor = Executors.newFixedThreadPool(2);
22-
IntStream.range(0, 10).forEach(i -> executor.submit(() -> accumulator.accumulate(i)));
23+
IntStream.range(0, 3).forEach(i -> executor.submit(() -> accumulator.accumulate(i)));
2324
ConcurrentUtils.stop(executor);
2425
System.out.format("Add: %d\n", accumulator.getThenReset());
2526
}
26-
}
27+
}

src/main/java/com/winterbe/java8/samples/concurrent/LongAdder1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
public class LongAdder1 {
1212

13-
private static final int NUM_INCREMENTS = 10000;
13+
private static final int NUM_INCREMENTS = 3;
1414
private static LongAdder adder = new LongAdder();
1515

1616
public static void main(String[] args) {

src/main/java/com/winterbe/java8/samples/concurrent/Threads1.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
public class Threads1 {
99

1010
public static void main(String[] args) {
11-
test1();
12-
// test2();
13-
// test3();
11+
// test1();
12+
// test2();
13+
test3();
1414
}
1515

1616
@SuppressWarnings("unused")

src/main/java/com/winterbe/java8/samples/concurrent/executor/Executors1SingleThread.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
/**
88
* @author Benjamin Winterberg
9+
* @Message support termination when thread running
910
*/
1011
public class Executors1SingleThread {
1112

1213
public static void main(String[] args) {
13-
test1(2);
14+
test1(10);
1415
// test1(7);
1516
}
1617

@@ -43,4 +44,4 @@ static void stop(ExecutorService executor) {
4344
System.out.println("shutdown finished");
4445
}
4546
}
46-
}
47+
}

src/main/java/com/winterbe/java8/samples/concurrent/executor/Executors2FixedThread.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010
/**
1111
* @author Benjamin Winterberg
12+
* @Message future
1213
*/
1314
@SuppressWarnings("unused")
1415
public class Executors2FixedThread {
1516

1617
public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
17-
test1();
18+
test1();
1819
// test2();
1920
test3();
2021
}
@@ -71,5 +72,4 @@ private static void test1() throws InterruptedException, ExecutionException {
7172

7273
executor.shutdownNow();
7374
}
74-
7575
}

src/main/java/com/winterbe/java8/samples/concurrent/executor/Executors3ScheduledStealingThread.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@
1212

1313
/**
1414
* @author Benjamin Winterberg
15+
* @Message stealing
1516
*/
1617
public class Executors3ScheduledStealingThread {
1718

1819
public static void main(String[] args) throws InterruptedException, ExecutionException {
19-
// test1();
20+
test1();
2021
// test2();
21-
// test3();
22-
23-
// test4();
24-
test5();
22+
// test3();
23+
// test4();
24+
// test5();
2525
}
2626

27+
/**
28+
* @Message 自动判断执行任何一个thread
29+
*/
2730
@SuppressWarnings("unused")
2831
private static void test5() throws InterruptedException, ExecutionException {
2932
ExecutorService executor = Executors.newWorkStealingPool();
3033

31-
List<Callable<String>> callables = Arrays.asList(callable("task1", 2), callable("task2", 4), callable("task3", 3));
34+
List<Callable<String>> callables = Arrays.asList(callable("task1", 9), callable("task2", 4), callable("task3", 3));
3235

3336
String result = executor.invokeAny(callables);
3437
System.out.println(result);
@@ -43,6 +46,10 @@ private static Callable<String> callable(String result, long sleepSeconds) {
4346
};
4447
}
4548

49+
/**
50+
* @Message 逐个执行 thread
51+
* @throws InterruptedException
52+
*/
4653
@SuppressWarnings("unused")
4754
private static void test4() throws InterruptedException {
4855
ExecutorService executor = Executors.newWorkStealingPool();
@@ -60,22 +67,28 @@ private static void test4() throws InterruptedException {
6067
executor.shutdown();
6168
}
6269

70+
/**
71+
* @Menssage 周期执行task
72+
*/
6373
@SuppressWarnings("unused")
6474
private static void test3() {
6575
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
6676

6777
Runnable task = () -> {
6878
try {
6979
TimeUnit.SECONDS.sleep(2);
70-
System.out.println("Scheduling: " + System.nanoTime());
80+
System.out.println("Scheduling: " + System.nanoTime() + "," + System.currentTimeMillis());
7181
} catch (InterruptedException e) {
7282
System.err.println("task interrupted");
7383
}
7484
};
7585

76-
executor.scheduleWithFixedDelay(task, 0, 1, TimeUnit.SECONDS);
86+
executor.scheduleWithFixedDelay(task, 0, 10, TimeUnit.SECONDS);
7787
}
7888

89+
/**
90+
* delay and period
91+
*/
7992
@SuppressWarnings("unused")
8093
private static void test2() {
8194
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
@@ -85,12 +98,16 @@ private static void test2() {
8598
executor.scheduleAtFixedRate(task, initialDelay, period, TimeUnit.SECONDS);
8699
}
87100

101+
/**
102+
* @Message delay
103+
* @throws InterruptedException
104+
*/
88105
private static void test1() throws InterruptedException {
89106
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
90107

91108
Runnable task = () -> System.out.println("Scheduling: " + System.nanoTime());
92109
int delay = 3;
93-
ScheduledFuture<?> future = executor.schedule(task, delay, TimeUnit.SECONDS);
110+
ScheduledFuture<?> future = executor.schedule(task, 3, TimeUnit.SECONDS);
94111

95112
TimeUnit.MILLISECONDS.sleep(1337);
96113

src/main/java/com/winterbe/java8/samples/concurrent/lock/Lock6.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/lock/ConvertToWriteLockDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @author Benjamin Winterberg
1111
*/
12-
public class Lock6 {
12+
public class ConvertToWriteLockDemo {
1313

1414
private static int count = 0;
1515

src/main/java/com/winterbe/java8/samples/concurrent/lock/Lock2.java renamed to src/main/java/com/winterbe/java8/samples/concurrent/lock/LambdaLock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* @author Benjamin Winterberg
1111
*/
12-
public class Lock2 {
12+
public class LambdaLock {
1313

1414
public static void main(String[] args) {
1515
ExecutorService executor = Executors.newFixedThreadPool(2);

0 commit comments

Comments
 (0)