Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 07fe3ae

Browse filesBrowse files
author
yanbit
committed
thread demo
1 parent 3475293 commit 07fe3ae
Copy full SHA for 07fe3ae

File tree

Expand file treeCollapse file tree

13 files changed

+444
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

13 files changed

+444
-0
lines changed
Open diff view settings
Collapse file
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.yanbit.source.study;
2+
/**
3+
* @author yanbit
4+
* @date Aug 25, 2015 2:24:08 PM
5+
*
6+
*/
7+
public class CPUcore {
8+
public static void main(String[] args) {
9+
System.out.println(Runtime.getRuntime().availableProcessors());
10+
System.out.println(Runtime.getRuntime().maxMemory());
11+
}
12+
}
Collapse file
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.yanbit.thread.c10;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author yanbit
7+
* @date Aug 25, 2015 4:58:24 PM
8+
*
9+
*/
10+
public class LeftRightDeadlock {
11+
private static final Object left = new Object();
12+
private static final Object right = new Object();
13+
14+
public static void leftRight() throws InterruptedException {
15+
synchronized (left) {
16+
TimeUnit.SECONDS.sleep(2);
17+
System.out.println("------");
18+
synchronized (right) {
19+
System.out.println("left right");
20+
}
21+
}
22+
}
23+
24+
public static void rightLeft() throws InterruptedException {
25+
synchronized (right) {
26+
TimeUnit.SECONDS.sleep(2);
27+
System.out.println("------");
28+
synchronized (left) {
29+
System.out.println("right left");
30+
}
31+
}
32+
}
33+
34+
public static void main(String[] args) {
35+
Thread t1 = new Thread() {
36+
@Override
37+
public void run() {
38+
try {
39+
leftRight();
40+
} catch (InterruptedException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
};
45+
46+
Thread t2 = new Thread() {
47+
@Override
48+
public void run() {
49+
try {
50+
rightLeft();
51+
} catch (InterruptedException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
};
56+
t1.start();
57+
t2.start();
58+
}
59+
}
Collapse file
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.yanbit.thread.c6;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.Callable;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.ExecutorService;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.Future;
9+
import java.util.concurrent.FutureTask;
10+
11+
/**
12+
* @author yanbit
13+
* @date Aug 21, 2015 11:20:53 AM
14+
*
15+
*/
16+
public class CallableDemo {
17+
public static void main(String[] args) throws InterruptedException, ExecutionException {
18+
ExecutorService service = Executors.newSingleThreadExecutor();
19+
Future f = service.submit(new Callable<Integer>(
20+
) {
21+
@Override
22+
public Integer call() throws Exception {
23+
return new Random().nextInt(10);
24+
}
25+
});
26+
System.out.println(f.get());
27+
}
28+
}
Collapse file
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.yanbit.thread.c6;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.Callable;
5+
import java.util.concurrent.ExecutionException;
6+
import java.util.concurrent.FutureTask;
7+
8+
/**
9+
* @author yanbit
10+
* @date Aug 21, 2015 11:20:53 AM
11+
*
12+
*/
13+
public class CallableDemo2 {
14+
public static void main(String[] args) {
15+
Callable<Integer> call = new Callable<Integer>() {
16+
@Override
17+
public Integer call() throws Exception {
18+
return new Random().nextInt(10);
19+
}
20+
};
21+
FutureTask<Integer> future = new FutureTask<Integer>(call);
22+
new Thread(future).start();
23+
try {
24+
Thread.sleep(2000);
25+
System.out.println(future.get());
26+
} catch (InterruptedException e) {
27+
e.printStackTrace();
28+
} catch (ExecutionException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
}
Collapse file
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.yanbit.thread.c6;
2+
3+
import java.util.Random;
4+
import java.util.concurrent.Callable;
5+
import java.util.concurrent.CompletionService;
6+
import java.util.concurrent.ExecutionException;
7+
import java.util.concurrent.ExecutorCompletionService;
8+
import java.util.concurrent.ExecutorService;
9+
import java.util.concurrent.Executors;
10+
import java.util.concurrent.TimeUnit;
11+
12+
/**
13+
* @author yanbit
14+
* @date Aug 21, 2015 11:20:53 AM
15+
*
16+
*/
17+
public class CallableDemo3 {
18+
public static void main(String[] args) {
19+
ExecutorService threadPool = Executors.newCachedThreadPool();
20+
CompletionService<Integer> cs = new ExecutorCompletionService<Integer>(threadPool);
21+
for(int i = 1; i < 5; i++) {
22+
final int taskID = i;
23+
cs.submit(new Callable<Integer>() {
24+
public Integer call() throws Exception {
25+
TimeUnit.SECONDS.sleep(new Random().nextInt(10));
26+
return taskID;
27+
}
28+
});
29+
}
30+
// 可能做一些事情
31+
for(int i = 1; i < 5; i++) {
32+
try {
33+
System.out.println(cs.take().get());
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
} catch (ExecutionException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
}
41+
}
Collapse file
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.yanbit.thread.c6;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
/**
7+
* @author yanbit
8+
* @date Aug 21, 2015 10:16:36 AM
9+
*
10+
*/
11+
public class ExecutorDemo {
12+
public static void main(String[] args) {
13+
ExecutorService threadService = Executors.newFixedThreadPool(10);
14+
threadService.execute(new Runnable() {
15+
@Override
16+
public void run() {
17+
System.out.println("hello");
18+
}
19+
});
20+
threadService.shutdown();
21+
}
22+
}
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.yanbit.thread.c6;
2+
3+
import java.util.concurrent.Executor;
4+
5+
/**
6+
* @author yanbit
7+
* @date Aug 21, 2015 10:20:37 AM
8+
*
9+
*/
10+
public class OExecutorDemo implements Executor{
11+
12+
@Override
13+
public void execute(Runnable command) {
14+
new Thread(command).start();
15+
}
16+
}
Collapse file
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.yanbit.thread.c6;
2+
3+
import java.util.Timer;
4+
import java.util.TimerTask;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* @author yanbit
9+
* @date Aug 21, 2015 10:52:05 AM
10+
*
11+
*/
12+
public class OutofTimer {
13+
public static void main(String[] args) throws InterruptedException {
14+
Timer timer = new Timer();
15+
timer.schedule(new Throw(), 1);
16+
TimeUnit.SECONDS.sleep(1);
17+
timer.schedule(new Throw(), 5);
18+
TimeUnit.SECONDS.sleep(5);
19+
}
20+
}
21+
22+
class Throw extends TimerTask{
23+
24+
@Override
25+
public void run() {
26+
throw new RuntimeException();
27+
}
28+
29+
}
Collapse file
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.yanbit.thread.c6;
2+
3+
import java.util.concurrent.Executor;
4+
5+
/**
6+
* @author yanbit
7+
* @date Aug 21, 2015 10:22:35 AM
8+
*
9+
*/
10+
public class SExecutorDemo implements Executor{
11+
12+
@Override
13+
public void execute(Runnable command) {
14+
new Thread(command).run();
15+
}
16+
}
Collapse file
+68Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.yanbit.thread.c7;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.InputStreamReader;
6+
7+
/**
8+
* @author yanbit
9+
* @date Aug 24, 2015 11:56:06 AM
10+
*
11+
*/
12+
public class FileScanner {
13+
private static void listFile(File f) throws InterruptedException {
14+
if (f == null) {
15+
throw new IllegalArgumentException();
16+
}
17+
if (f.isFile()) {
18+
System.out.println(f);
19+
return;
20+
}
21+
File[] allFiles = f.listFiles();
22+
if (Thread.interrupted()) {
23+
throw new InterruptedException("文件扫描任务被中断");
24+
}
25+
for (File file : allFiles) {
26+
// 还可以将中断检测放到这里
27+
listFile(file);
28+
}
29+
}
30+
31+
public static String readFromConsole() {
32+
BufferedReader reader =
33+
new BufferedReader(new InputStreamReader(System.in));
34+
try {
35+
return reader.readLine();
36+
} catch (Exception e) {
37+
e.printStackTrace();
38+
return "";
39+
}
40+
}
41+
42+
public static void main(String[] args) throws Exception {
43+
final Thread fileIteratorThread = new Thread() {
44+
public void run() {
45+
try {
46+
listFile(new File("c:\\"));
47+
} catch (InterruptedException e) {
48+
e.printStackTrace();
49+
}
50+
}
51+
};
52+
new Thread() {
53+
public void run() {
54+
while (true) {
55+
if ("quit".equalsIgnoreCase(readFromConsole())) {
56+
if (fileIteratorThread.isAlive()) {
57+
fileIteratorThread.interrupt();
58+
return;
59+
}
60+
} else {
61+
System.out.println("输入quit退出文件扫描");
62+
}
63+
}
64+
}
65+
}.start();
66+
fileIteratorThread.start();
67+
}
68+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.