Skip to content

Commit be2c950

Browse files
author
yuki
committed
[wip]ch 21.2
1 parent af7cc33 commit be2c950

25 files changed

Lines changed: 742 additions & 9 deletions

src/main/java/ch17/SList.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ch17;
22

3-
import lombok.Getter;
4-
53
import java.util.Iterator;
64

75
/**
@@ -15,11 +13,17 @@ public class SList<T> {
1513

1614

1715
private class Node<T> {
18-
@Getter
1916
private T value;
20-
@Getter
2117
private Node<T> next;
2218

19+
public T getValue() {
20+
return value;
21+
}
22+
23+
public Node<T> getNext() {
24+
return next;
25+
}
26+
2327
public Node(T t) {
2428
this.value = t;
2529
this.next = null;
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package ch20.unitTest;
22

3-
import lombok.AllArgsConstructor;
4-
53
import java.io.File;
6-
import java.io.IOException;
74

85
/**
96
* @author yuzhe
107
* @since 10/9/18
118
*/
12-
@AllArgsConstructor
139
public class ProcessFiles {
1410

1511
//纯粹为了使用接口从ch18复制过来,lombok插件和编译期注解蜜汁冲突
@@ -20,4 +16,8 @@ public interface Strategy {
2016
private Strategy strategy;
2117
private String ext;
2218

19+
public ProcessFiles(Strategy strategy, String ext) {
20+
this.strategy = strategy;
21+
this.ext = ext;
22+
}
2323
}

src/main/java/ch21/ADaemon.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ch21;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 11/12/18
8+
*/
9+
public class ADaemon implements Runnable{
10+
11+
12+
@Override
13+
public void run() {
14+
try{
15+
System.out.println("starting ADaemon.");
16+
TimeUnit.SECONDS.sleep(1);
17+
} catch (InterruptedException e) {
18+
e.printStackTrace();
19+
} finally {
20+
System.out.println("finally runs.");
21+
}
22+
}
23+
24+
public static void main(String[] args) {
25+
Thread t = new Thread(new ADaemon());
26+
t.setDaemon(true);
27+
t.start();
28+
}
29+
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ch21;
2+
3+
/**
4+
* @author yuzhe
5+
* @since 11/12/18
6+
*/
7+
public class BasicThreads {
8+
9+
public static void main(String[] args) {
10+
Thread t = new Thread(new LiftOff());
11+
t.start();
12+
System.out.println("waiting for liftoff");
13+
}
14+
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ch21;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
/**
7+
* @author yuzhe
8+
* @since 11/12/18
9+
*/
10+
public class CacheThreadPool {
11+
12+
public static void main(String[] args) {
13+
ExecutorService service = Executors.newCachedThreadPool();
14+
for(int i = 0; i < 5; i++){
15+
service.execute(new LiftOff());
16+
}
17+
service.shutdown();
18+
}
19+
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ch21;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.Future;
7+
import java.util.ArrayList;
8+
/**
9+
* @author yuzhe
10+
* @since 11/12/18
11+
*/
12+
public class CallabledDemo {
13+
14+
public static void main(String[] args) {
15+
ExecutorService exec = Executors.newCachedThreadPool();
16+
ArrayList<Future<String>> results = new ArrayList<>();
17+
for(int i = 0; i < 10; i++){
18+
results.add(exec.submit(new TaskWithResult(i)));
19+
}
20+
for(Future<String> result : results){
21+
try {
22+
System.out.println(result.get());
23+
} catch (InterruptedException | ExecutionException e) {
24+
e.printStackTrace();
25+
} finally {
26+
exec.shutdown();
27+
}
28+
}
29+
}
30+
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package ch21;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* @author yuzhe
9+
* @since 11/12/18
10+
*/
11+
public class DaemonFromFactory implements Runnable {
12+
@Override
13+
public void run() {
14+
try{
15+
while (true){
16+
TimeUnit.MILLISECONDS.sleep(100l);
17+
System.out.println(Thread.currentThread() + " " + this);
18+
}
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
System.out.println("sleep interrupted");
22+
}
23+
}
24+
25+
public static void main(String[] args) throws InterruptedException {
26+
ExecutorService exec = Executors.newCachedThreadPool(new DaemonThreadFactory());
27+
for(int i = 0; i < 10; i++){
28+
exec.execute(new DaemonFromFactory());
29+
}
30+
System.out.println("all daemons started");
31+
TimeUnit.MILLISECONDS.sleep(500);
32+
}
33+
34+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ch21;
2+
3+
import java.util.concurrent.ThreadFactory;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 11/12/18
8+
*/
9+
public class DaemonThreadFactory implements ThreadFactory {
10+
@Override
11+
public Thread newThread(Runnable r) {
12+
Thread thread = new Thread(r);
13+
thread.setDaemon(true);
14+
return thread;
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ch21;
2+
3+
import java.util.concurrent.SynchronousQueue;
4+
import java.util.concurrent.ThreadPoolExecutor;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* @author yuzhe
9+
* @since 11/12/18
10+
*/
11+
public class DaemonThreadPoolExecutor extends ThreadPoolExecutor {
12+
13+
public DaemonThreadPoolExecutor() {
14+
super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
15+
new SynchronousQueue<Runnable>(),
16+
new DaemonThreadFactory()
17+
);
18+
}
19+
}

src/main/java/ch21/Daemons.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package ch21;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author yuzhe
7+
* @since 11/12/18
8+
*/
9+
public class Daemons {
10+
11+
public static void main(String[] args) throws InterruptedException {
12+
Thread d = new Thread(new Daemon());
13+
d.setDaemon(true);
14+
d.start();
15+
System.out.println("d.isDaemon() = " + d.isDaemon());
16+
TimeUnit.SECONDS.sleep(1);
17+
}
18+
19+
}
20+
21+
class Daemon implements Runnable{
22+
private Thread[] t = new Thread[10];
23+
24+
@Override
25+
public void run() {
26+
for(int i = 0; i < t.length; i++){
27+
t[i] = new Thread(new DaemonSpawn());
28+
t[i].start();
29+
System.out.println("DaemonSpawn " + i + " started.");
30+
}
31+
for(int i = 0; i < t.length; i++){
32+
System.out.println("t[" + i + "].isDaemon() = " + t[i].isDaemon());
33+
}
34+
while (true){
35+
Thread.yield();
36+
}
37+
}
38+
}
39+
40+
class DaemonSpawn implements Runnable{
41+
42+
@Override
43+
public void run() {
44+
while(true){
45+
Thread.yield();
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)