Skip to content

Commit ca2c723

Browse files
committed
Concurrency samples
1 parent d2b66c3 commit ca2c723

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.winterbe.java8.samples.concurrent;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* @author Benjamin Winterberg
7+
*/
8+
public class Threads1 {
9+
10+
public static void main(String[] args) {
11+
test1();
12+
test2();
13+
test3();
14+
}
15+
16+
private static void test3() {
17+
Runnable runnable = () -> {
18+
try {
19+
System.out.println("Foo " + Thread.currentThread().getName());
20+
TimeUnit.SECONDS.sleep(1);
21+
System.out.println("Bar " + Thread.currentThread().getName());
22+
}
23+
catch (InterruptedException e) {
24+
e.printStackTrace();
25+
}
26+
};
27+
28+
Thread thread = new Thread(runnable);
29+
thread.start();
30+
}
31+
32+
private static void test2() {
33+
Runnable runnable = () -> {
34+
try {
35+
System.out.println("Foo " + Thread.currentThread().getName());
36+
Thread.sleep(1000);
37+
System.out.println("Bar " + Thread.currentThread().getName());
38+
}
39+
catch (InterruptedException e) {
40+
e.printStackTrace();
41+
}
42+
};
43+
44+
Thread thread = new Thread(runnable);
45+
thread.start();
46+
}
47+
48+
private static void test1() {
49+
Runnable runnable = () -> System.out.println("Hello " + Thread.currentThread().getName());
50+
51+
runnable.run();
52+
53+
Thread thread = new Thread(runnable);
54+
thread.start();
55+
56+
System.out.println("Nachos!");
57+
}
58+
}

0 commit comments

Comments
 (0)