forked from winterbe/java8-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreads1.java
More file actions
61 lines (50 loc) · 1.53 KB
/
Threads1.java
File metadata and controls
61 lines (50 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.winterbe.java8.samples.concurrent;
import java.util.concurrent.TimeUnit;
/**
* @author Benjamin Winterberg
*/
public class Threads1 {
public static void main(String[] args) {
test1();
// test2();
// test3();
}
private static void test3() {
Runnable runnable = () -> {
try {
System.out.println("Foo " + Thread.currentThread().getName());
TimeUnit.SECONDS.sleep(1);
System.out.println("Bar " + Thread.currentThread().getName());
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
}
private static void test2() {
Runnable runnable = () -> {
try {
System.out.println("Foo " + Thread.currentThread().getName());
Thread.sleep(1000);
System.out.println("Bar " + Thread.currentThread().getName());
}
catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
}
private static void test1() {
Runnable runnable = () -> {
String threadName = Thread.currentThread().getName();
System.out.println("Hello " + threadName);
};
runnable.run();
Thread thread = new Thread(runnable);
thread.start();
System.out.println("Done!");
}
}