Skip to content

Commit 96523bb

Browse files
committed
并发编程部分代码 原子类之前的代码
1 parent 79a0a72 commit 96523bb

22 files changed

Lines changed: 819 additions & 0 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package concurrency;
2+
3+
import org.omg.CORBA.TIMEOUT;
4+
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.locks.ReentrantLock;
7+
8+
/**
9+
* Lock的使用
10+
*
11+
* @author :chenxin
12+
* @date :Created in 2020/3/15 16:55
13+
* @modified By:
14+
* @version: $
15+
*/
16+
public class AttemptLocking {
17+
private ReentrantLock lock = new ReentrantLock();
18+
19+
public void untimed() {
20+
boolean captured = lock.tryLock();
21+
try {
22+
System.out.println("tryLock():" + captured);
23+
} finally {
24+
if (captured) {
25+
lock.unlock();
26+
}
27+
}
28+
}
29+
30+
public void timed() {
31+
boolean captured = false;
32+
try {
33+
captured = lock.tryLock(2, TimeUnit.SECONDS );
34+
} catch (InterruptedException e) {
35+
throw new RuntimeException();
36+
}
37+
try {
38+
System.out.println("tryLock(2,TimeUnit.SECONDS): " + captured);
39+
} finally {
40+
if (captured) {
41+
lock.unlock();
42+
}
43+
}
44+
}
45+
46+
public static void main(String[] args) throws InterruptedException {
47+
final AttemptLocking al = new AttemptLocking();
48+
al.untimed();
49+
al.timed();
50+
new Thread() {
51+
{
52+
setDaemon(true);
53+
}
54+
55+
public void run() {
56+
al.lock.lock();
57+
System.out.println("acquired");
58+
}
59+
}.start();
60+
Thread.sleep(1000);
61+
al.untimed();
62+
al.timed();
63+
}
64+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package concurrency;
2+
3+
/**
4+
* @author 10745
5+
* @date 2020/3/13 21:15
6+
**/
7+
public class BasicThreads {
8+
public static void main(String[] args) {
9+
final Thread thread = new Thread(new LiftOff());
10+
thread.start();
11+
System.out.print("Waiting for LifeOff");
12+
13+
}
14+
}
15+
16+
class MoreBasicThreads {
17+
public static void main(String[] args) {
18+
for (int i = 0; i < 5; i++) {
19+
new Thread((new LiftOff())).start();
20+
}
21+
System.out.println("Waiting for LifeOff");
22+
}
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package concurrency;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
/**
7+
* @author 10745
8+
* @date 2020/3/13 21:40
9+
**/
10+
public class CachedThreadPool {
11+
public static void main(String[] args) {
12+
13+
final ExecutorService executorService = Executors.newCachedThreadPool();
14+
15+
for (int i = 0; i < 5; i++) {
16+
executorService.execute(new LiftOff());
17+
}
18+
executorService.shutdown();
19+
}
20+
}
21+
22+
class FixedThreadPool {
23+
public static void main(String[] args) {
24+
25+
final ExecutorService executorService = Executors.newFixedThreadPool(5);
26+
27+
for (int i = 0; i < 5; i++) {
28+
executorService.execute(new LiftOff());
29+
}
30+
executorService.shutdown();
31+
}
32+
}
33+
34+
class SingleThreadExecutor {
35+
public static void main(String[] args) {
36+
37+
final ExecutorService executorService = Executors.newSingleThreadExecutor();
38+
39+
for (int i = 0; i < 5; i++) {
40+
executorService.execute(new LiftOff());
41+
}
42+
executorService.shutdown();
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package concurrency;
2+
3+
import java.util.ArrayList;
4+
import java.util.concurrent.*;
5+
6+
/**
7+
* @author 10745
8+
* @date 2020/3/13 22:01
9+
**/
10+
public class CallableDemo {
11+
public static void main(String[] args) {
12+
final ExecutorService executorService = Executors.newCachedThreadPool();
13+
final ArrayList<Future<String>> results = new ArrayList<>();
14+
for (int i = 0; i < 10; i++) {
15+
results.add(executorService.submit(new TaskWithResult(i)));
16+
}
17+
for (Future<String> fs : results) {
18+
try {
19+
System.out.println(fs.get());
20+
} catch (InterruptedException e) {
21+
System.out.println(e);
22+
return;
23+
} catch (ExecutionException e) {
24+
System.out.println(e);
25+
} finally {
26+
executorService.shutdown();
27+
}
28+
}
29+
}
30+
}
31+
32+
class TaskWithResult implements Callable<String> {
33+
34+
private int id;
35+
36+
public TaskWithResult(int id) {
37+
this.id = id;
38+
}
39+
40+
@Override
41+
public String call() throws Exception {
42+
return "result of TaskWithResult " + id;
43+
}
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package concurrency;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.ThreadFactory;
6+
7+
/**
8+
* 捕获线程中的异常
9+
*
10+
* @author :chenxin
11+
* @date :Created in 2020/3/14 23:27
12+
* @modified By:
13+
* @version: $
14+
*/
15+
public class CaptureUncaughtException {
16+
public static void main(String[] args) {
17+
final ExecutorService executorService = Executors.newCachedThreadPool(new HandlerThreadFactory());
18+
executorService.execute(new ExceptionThread2());
19+
}
20+
}
21+
22+
class ExceptionThread2 implements Runnable {
23+
@Override
24+
public void run() {
25+
final Thread t = Thread.currentThread();
26+
System.out.println("run() by " + t);
27+
System.out.println("eh = " + t.getUncaughtExceptionHandler());
28+
throw new RuntimeException();
29+
}
30+
}
31+
32+
class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
33+
34+
@Override
35+
public void uncaughtException(Thread t, Throwable e) {
36+
System.out.println("caught " + e);
37+
}
38+
}
39+
40+
class HandlerThreadFactory implements ThreadFactory {
41+
42+
@Override
43+
public Thread newThread(Runnable r) {
44+
System.out.println(this + " creating new Thread");
45+
Thread t = new Thread(r);
46+
System.out.println("created " + t);
47+
t.setUncaughtExceptionHandler(new MyUncaughtExceptionHandler());
48+
System.out.println("eh = " + t.getUncaughtExceptionHandler());
49+
return t;
50+
}
51+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package concurrency;
2+
3+
import java.util.concurrent.*;
4+
5+
/**
6+
* @author 10745
7+
* @date 2020/3/13 23:07
8+
**/
9+
public class DaemonThreadPoolExecutor extends ThreadPoolExecutor {
10+
11+
public DaemonThreadPoolExecutor() {
12+
super(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new DeamonThreadFactory());
13+
}
14+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package concurrency;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author 10745
7+
* @date 2020/3/13 23:34
8+
**/
9+
public class Daemons {
10+
public static void main(String[] args) throws InterruptedException {
11+
final Thread d = new Thread(new Daemon());
12+
d.setDaemon(true);
13+
d.start();
14+
System.out.println("d.isDaemon() = " + d.isDaemon());
15+
TimeUnit.MILLISECONDS.sleep(100);
16+
}
17+
}
18+
19+
class Daemon implements Runnable {
20+
private Thread[] threads = new Thread[10];
21+
22+
@Override
23+
public void run() {
24+
for (int i = 0; i < threads.length; i++) {
25+
threads[i] = new Thread(new DaemonSpawn());
26+
threads[i].start();
27+
System.out.println("DaemonSpawn " + i + " started,");
28+
}
29+
for (int i = 0; i < threads.length; i++) {
30+
System.out.print(" t[" + i + "].isDaemon()= " + threads[i].isDaemon());
31+
}
32+
while (true) {
33+
Thread.yield();
34+
}
35+
}
36+
}
37+
38+
class DaemonSpawn implements Runnable {
39+
@Override
40+
public void run() {
41+
while (true) {
42+
Thread.yield();
43+
}
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package concurrency;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* @author 10745
9+
* @date 2020/3/13 23:00
10+
**/
11+
public class DeamonFromFactory implements Runnable {
12+
@Override
13+
public void run() {
14+
try {
15+
while (true) {
16+
TimeUnit.MILLISECONDS.sleep(100);
17+
System.out.println(Thread.currentThread() + " " + this);
18+
}
19+
} catch (InterruptedException e) {
20+
System.out.println(e);
21+
}
22+
}
23+
24+
public static void main(String[] args) throws InterruptedException {
25+
final ExecutorService executorService = Executors.newCachedThreadPool(new DeamonThreadFactory());
26+
for (int i = 0; i < 10; i++) {
27+
executorService.execute(new DeamonFromFactory());
28+
}
29+
System.out.println("All daemons started");
30+
TimeUnit.MILLISECONDS.sleep(500);
31+
}
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package concurrency;
2+
3+
import java.util.concurrent.ThreadFactory;
4+
5+
/**
6+
* @author 10745
7+
* @date 2020/3/13 22:57
8+
**/
9+
public class DeamonThreadFactory 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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package concurrency;
2+
3+
import genericity.Generator;
4+
5+
import javax.lang.model.element.Element;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
9+
/**
10+
* 共享资源的不正确访问示例
11+
*
12+
* @author :chenxin
13+
* @date :Created in 2020/3/15 15:35
14+
* @modified By:
15+
* @version: $
16+
*/
17+
public class EvenChecker implements Runnable {
18+
19+
private IntGenerator generator;
20+
21+
private final int id;
22+
23+
public EvenChecker(IntGenerator generator, int id) {
24+
this.generator = generator;
25+
this.id = id;
26+
}
27+
28+
@Override
29+
public void run() {
30+
while (!generator.isCanceled()) {
31+
int val = generator.next();
32+
if (val % 2 != 0) {
33+
System.out.println(val + " not even");
34+
generator.cancle();
35+
}
36+
}
37+
}
38+
39+
public static void test(IntGenerator generator, int count) {
40+
System.out.println("Press Control-C to exit ");
41+
final ExecutorService executorService = Executors.newCachedThreadPool();
42+
for (int i = 0; i < count; i++) {
43+
executorService.execute(new EvenChecker(generator, i));
44+
}
45+
executorService.shutdown();
46+
}
47+
48+
public static void test(IntGenerator generator) {
49+
test(generator, 10);
50+
}
51+
}

0 commit comments

Comments
 (0)