diff --git a/src/createthread/CreateByAnonymousClass.java b/src/createthread/CreateByAnonymousClass.java new file mode 100644 index 0000000..6643a7d --- /dev/null +++ b/src/createthread/CreateByAnonymousClass.java @@ -0,0 +1,17 @@ +package createthread; + +public class CreateByAnonymousClass { + + public static void main(String[] args) { + new Thread(new Runnable() { + @Override + public void run() { + System.out.println("create thread by using anonymous class"); + } + }).start(); + + new Thread(() -> { + System.out.println("create thread by using lambda"); + }).start(); + } +} diff --git a/src/createthread/CreateByCompletableFuture.java b/src/createthread/CreateByCompletableFuture.java new file mode 100644 index 0000000..f4409c0 --- /dev/null +++ b/src/createthread/CreateByCompletableFuture.java @@ -0,0 +1,30 @@ +package createthread; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +public class CreateByCompletableFuture { + + public static void main(String[] args) throws ExecutionException, InterruptedException { + CompletableFuture cf = CompletableFuture.supplyAsync(() -> { + System.out.println("create thread by using CompletableFuture. Returning results..."); + return "hello!"; + }); + + while (true) { + if (cf.isDone()) { + System.out.println(cf.get()); + break; + } + } + +// try { +// Thread.sleep(0); +// System.out.println(cf.get()); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } catch (ExecutionException e) { +// throw new RuntimeException(e); +// } + } +} diff --git a/src/createthread/CreateByForkJoin.java b/src/createthread/CreateByForkJoin.java new file mode 100644 index 0000000..5fd769d --- /dev/null +++ b/src/createthread/CreateByForkJoin.java @@ -0,0 +1,18 @@ +package createthread; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ForkJoinPool; + +public class CreateByForkJoin { + + public static void main(String[] args) { + ForkJoinPool forkJoinPool = new ForkJoinPool(); + forkJoinPool.execute(() -> { + System.out.println("create thread by using forkjoin 10A..."); + }); + + List list = Arrays.asList("10B......"); + list.parallelStream().forEach(System.out::println); + } +} diff --git a/src/createthread/CreateByFutureTask.java b/src/createthread/CreateByFutureTask.java new file mode 100644 index 0000000..1dcbfa3 --- /dev/null +++ b/src/createthread/CreateByFutureTask.java @@ -0,0 +1,18 @@ +package createthread; + +import java.util.concurrent.FutureTask; + +public class CreateByFutureTask { + public static void main(String[] args) + { + FutureTask futureTask = new FutureTask<>(() -> "Create by FutureTask"); + + new Thread(futureTask).start(); + + try { + System.out.println(futureTask.get()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/createthread/CreateByThreadGroup.java b/src/createthread/CreateByThreadGroup.java new file mode 100644 index 0000000..2c907d0 --- /dev/null +++ b/src/createthread/CreateByThreadGroup.java @@ -0,0 +1,18 @@ +package createthread; + +public class CreateByThreadGroup { + + public static void main(String[] args) { + ThreadGroup threadGroup = new ThreadGroup("threadGroup"); + + new Thread(threadGroup, () -> { + System.out.println("6-T1......"); + },"T1").start(); + new Thread(threadGroup, () -> { + System.out.println("6-T2......"); + },"T2").start(); + new Thread(threadGroup, () -> { + System.out.println("6-T3......"); + },"T3").start(); + } +} diff --git a/src/createthread/CreateByTimer.java b/src/createthread/CreateByTimer.java new file mode 100644 index 0000000..b4222bb --- /dev/null +++ b/src/createthread/CreateByTimer.java @@ -0,0 +1,18 @@ +package createthread; + +import java.util.Timer; +import java.util.TimerTask; + +public class CreateByTimer { + + public static void main(String[] args) { + Timer timer = new Timer(); + + timer.schedule(new TimerTask() { + @Override + public void run() { + System.out.println("create thread by using timer"); + } + }, 0, 1000); + } +}