提交 ca2c7238 编写于 作者: B Benjamin Winterberg

Concurrency samples

上级 d2b66c31
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 = () -> System.out.println("Hello " + Thread.currentThread().getName());
runnable.run();
Thread thread = new Thread(runnable);
thread.start();
System.out.println("Nachos!");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册