From ca2c72389649da5688b780746570a9c621b21ac1 Mon Sep 17 00:00:00 2001 From: Benjamin Winterberg Date: Thu, 26 Mar 2015 09:14:05 +0100 Subject: [PATCH] Concurrency samples --- .../java8/samples/concurrent/Threads1.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/com/winterbe/java8/samples/concurrent/Threads1.java diff --git a/src/com/winterbe/java8/samples/concurrent/Threads1.java b/src/com/winterbe/java8/samples/concurrent/Threads1.java new file mode 100644 index 0000000..2470aad --- /dev/null +++ b/src/com/winterbe/java8/samples/concurrent/Threads1.java @@ -0,0 +1,58 @@ +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!"); + } +} -- GitLab