From 153d221f362e9323e5eeca0b2f778e4a7a91fe9b Mon Sep 17 00:00:00 2001 From: binghe001 <1028386804@qq.com> Date: Sat, 20 Aug 2022 22:56:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AC=AC2=E7=AB=A0=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mykit-concurrent-chapter02/pom.xml | 16 + .../consurrent/chapter02/CallableTest.java | 67 ++++ .../consurrent/chapter02/ExecutorsTest.java | 57 ++++ .../consurrent/chapter02/RunnableTest.java | 58 ++++ .../chapter02/ThreadHandlerTest.java | 296 ++++++++++++++++++ .../chapter02/ThreadPoolExecutorTest.java | 55 ++++ .../consurrent/chapter02/ThreadTest.java | 41 +++ pom.xml | 9 + 8 files changed, 599 insertions(+) create mode 100644 mykit-concurrent-chapter02/pom.xml create mode 100644 mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/CallableTest.java create mode 100644 mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ExecutorsTest.java create mode 100644 mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/RunnableTest.java create mode 100644 mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadHandlerTest.java create mode 100644 mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadPoolExecutorTest.java create mode 100644 mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadTest.java diff --git a/mykit-concurrent-chapter02/pom.xml b/mykit-concurrent-chapter02/pom.xml new file mode 100644 index 0000000..5e770be --- /dev/null +++ b/mykit-concurrent-chapter02/pom.xml @@ -0,0 +1,16 @@ + + + + mykit-concurrent-jdk + io.binghe.concurrent + 1.0.0-SNAPSHOT + + 4.0.0 + + mykit-concurrent-chapter02 + + + + \ No newline at end of file diff --git a/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/CallableTest.java b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/CallableTest.java new file mode 100644 index 0000000..be2d69a --- /dev/null +++ b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/CallableTest.java @@ -0,0 +1,67 @@ +/** + * Copyright 2020-9999 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.binghe.consurrent.chapter02; + +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.FutureTask; + +/** + * @author binghe + * @version 1.0.0 + * @description 实现Callable接口创建线程 + */ +public class CallableTest { + + private static class MyCallbleTask implements Callable{ + @Override + public String call() throws Exception { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + return Thread.currentThread().getName(); + } + } + + /** + * 以匿名类的方式创建线程 + */ + public FutureTask createThreadByCallableAnonymousClass() { + return new FutureTask<>(new Callable() { + @Override + public String call() throws Exception { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + return Thread.currentThread().getName(); + } + }); + } + /** + * 以Lambda表达式的方式创建线程 + */ + public FutureTask createThreadByCallableLambda() { + return new FutureTask<>(() -> { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + return Thread.currentThread().getName(); + }); + } + + + + public static void main(String[] args) throws ExecutionException, InterruptedException { + FutureTask futureTask = new FutureTask(new MyCallbleTask()); + new Thread(futureTask).start(); + System.out.println("从子线程中获取到的数据为===>> " + futureTask.get()); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } +} diff --git a/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ExecutorsTest.java b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ExecutorsTest.java new file mode 100644 index 0000000..56f23f1 --- /dev/null +++ b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ExecutorsTest.java @@ -0,0 +1,57 @@ +/** + * Copyright 2020-9999 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.binghe.consurrent.chapter02; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +/** + * @author binghe + * @version 1.0.0 + * @description 通过Executors工具创建线程 + */ +public class ExecutorsTest { + + private static ExecutorService threadPool; + + static { + threadPool = Executors.newFixedThreadPool(3); + } + + public static void main1(String[] args) { + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + threadPool.submit(() -> { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + }); + } + public static void main2(String[] args) throws ExecutionException, InterruptedException { + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + Future future = threadPool.submit(() -> { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + return Thread.currentThread().getName(); + }); + System.out.println("从子线程中获取到的数据为===>> " + future.get()); + } + + public static void main(String[] args){ + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + threadPool.execute(() -> { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + }); + } +} diff --git a/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/RunnableTest.java b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/RunnableTest.java new file mode 100644 index 0000000..fece238 --- /dev/null +++ b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/RunnableTest.java @@ -0,0 +1,58 @@ +/** + * Copyright 2020-9999 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.binghe.consurrent.chapter02; + +/** + * @author binghe + * @version 1.0.0 + * @description 实现Runnable接口创建线程 + */ +public class RunnableTest{ + + private static class MyRunnableTask implements Runnable{ + @Override + public void run() { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + } + } + + /** + * 以匿名类的方式创建线程 + */ + public Thread createThreadByRunnableAnonymousClass(){ + return new Thread(new Runnable() { + @Override + public void run() { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + } + }); + } + + + /** + * 以Lambda表达式的方式创建线程 + */ + public Thread createThreadByRunnableLambda(){ + return new Thread(() -> { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + }); + } + + public static void main(String[] args) { + new Thread(new MyRunnableTask()).start(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } +} diff --git a/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadHandlerTest.java b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadHandlerTest.java new file mode 100644 index 0000000..3af37d6 --- /dev/null +++ b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadHandlerTest.java @@ -0,0 +1,296 @@ +/** + * Copyright 2020-9999 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.binghe.consurrent.chapter02; + +import java.util.Date; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +/** + * @author binghe + * @version 1.0.0 + * @description 线程的基本操作 + */ +public class ThreadHandlerTest { + + public static void main1(String[] args){ + Thread thread = new Thread(() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + },"binghe-thread-002"); + thread.start(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + public static void main2(String[] args){ + Thread thread = new Thread(() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + }); + thread.setName("binghe-thread-002"); + thread.start(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + public static void main3(String[] args){ + Thread thread = new Thread(new ThreadGroup("binghe-thread-002-group"),() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + }); + thread.start(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + System.out.println("子线程所在的线程组名称为===>>> " + thread.getThreadGroup().getName()); + } + + public static void main4(String[] args){ + Thread thread = new Thread(() -> { + System.out.println("当前时间为===>>> " + new Date()); + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + System.out.println("当前时间为===>>> " + new Date()); + }); + thread.start(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + public static void main5(String[] args) throws InterruptedException { + Thread thread = new Thread(() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + System.out.println("中断休眠中的线程会抛出异常,并清空中断标记,捕获异常后重新设置中断标记"); + Thread.currentThread().interrupt(); + } + }); + thread.start(); + //保证子线程已经启动完成 + Thread.currentThread().sleep(500); + System.out.println("在主线程中中断子线程"); + //中断子线程 + thread.interrupt(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + + public static void main6(String[] args) throws InterruptedException { + Thread thread = new Thread(() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + while (!Thread.currentThread().isInterrupted()){ + } + System.out.println("子线程退出了while循环"); + }); + thread.start(); + //保证子线程已经启动完成 + Thread.currentThread().sleep(500); + System.out.println("在主线程中中断子线程"); + //中断子线程 + thread.interrupt(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + public static void main7(String[] args) throws InterruptedException { + final Object obj = new Object(); + Thread thread = new Thread(() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + System.out.println("子线程等待"); + synchronized (obj){ + try { + obj.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + System.out.println("子线程被唤醒"); + }); + thread.start(); + //保证子线程已经启动完成 + Thread.currentThread().sleep(500); + System.out.println("主线程通知子线程"); + synchronized (obj){ + obj.notify(); + } + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + public static void main8(String[] args) throws InterruptedException { + final Object obj = new Object(); + Thread thread = new Thread(() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + synchronized (obj){ + System.out.println("子线程挂起"); + Thread.currentThread().suspend(); + } + System.out.println("子线程被唤醒"); + }); + thread.start(); + //保证子线程已经启动完成 + Thread.currentThread().sleep(500); + System.out.println("主线程通知子线程继续执行"); + thread.resume(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + private static int sum = 0; + public static void main9(String[] args) throws InterruptedException { + Thread thread = new Thread(() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + IntStream.range(0, 1000).forEach((i) -> {sum += 1;}); + }); + thread.start(); +// //保证子线程已经启动完成 +// Thread.currentThread().sleep(500); + thread.join(); + System.out.println("主线程获取到的结果为===>>> " + sum); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + + public static void main10(String[] args) throws InterruptedException { + Thread thread = new Thread(() -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + while (true){ + } + }); + System.out.println("启动子线程===>>> " + new Date()); + thread.start(); + //保证子线程已经启动完成 + Thread.currentThread().sleep(5000); + System.out.println("强制退出子线程===>>> " + new Date()); + thread.stop(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + public static void main11(String[] args) throws InterruptedException { + ThreadGroup threadGroup = new ThreadGroup("binghe-thread-group-002"); + ThreadGroup subThreadGroup = new ThreadGroup(threadGroup,"binghe-sub-thread-group-002"); + Thread thread1 = new Thread(threadGroup, () -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + try { + Thread.currentThread().sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + Thread thread2 = new Thread(subThreadGroup, () -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + try { + Thread.currentThread().sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + thread1.start(); + thread2.start(); + //保证子线程已经启动完成 + Thread.currentThread().sleep(500); + System.out.println("线程组中活跃的线程组数量为===>> " + threadGroup.activeGroupCount()); + System.out.println("线程组中活跃的线程数量为===>> " + threadGroup.activeCount()); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + public static void main12(String[] args) throws InterruptedException { + ThreadGroup threadGroup = new ThreadGroup("binghe-thread-group-002"); + ThreadGroup subThreadGroup = new ThreadGroup(threadGroup,"binghe-sub-thread-group-002"); + Thread thread1 = new Thread(threadGroup, () -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + try { + Thread.currentThread().sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + Thread thread2 = new Thread(subThreadGroup, () -> { + System.out.println("子线程名称===>> " + Thread.currentThread().getName()); + //加入到subThreadGroup线程组中 + ThreadGroup thread2Group = new ThreadGroup("binghe-thread2-group-002"); + try { + Thread.currentThread().sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + thread1.start(); + thread2.start(); + //保证子线程已经启动完成 + Thread.currentThread().sleep(500); + System.out.println("threadGroup线程组中活跃的线程组数量为===>> " + threadGroup.activeGroupCount()); + System.out.println("threadGroup线程组中活跃的线程数量为===>> " + threadGroup.activeCount()); + System.out.println("subThreadGroup线程组中活跃的线程组数量为===>> " + subThreadGroup.activeGroupCount()); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + public static void main13(String[] args) { + System.out.println(Thread.currentThread().getThreadGroup().getName()); + System.out.println(Thread.currentThread().getThreadGroup().getParent().getName()); + System.out.println(Thread.currentThread().getThreadGroup().getParent().getParent().getName()); + } + + public static void main14(String[] args) { + ThreadGroup threadGroup = new ThreadGroup("binghe-thread-group-002"); + System.out.println("threadGroup线程组中活跃的线程组数量为===>> " + threadGroup.activeGroupCount()); + ThreadGroup subThreadGroup = new ThreadGroup(threadGroup,"binghe-sub-thread-group-002"); + System.out.println("threadGroup线程组中活跃的线程组数量为===>> " + threadGroup.activeGroupCount()); + } + + public static void main15(String[] args) { + ThreadGroup mainGroup = Thread.currentThread().getThreadGroup(); + ThreadGroup threadGroup = new ThreadGroup(mainGroup, "threadGroup"); + ThreadGroup subThreadGroup1 = new ThreadGroup(threadGroup,"subThreadGroup1"); + ThreadGroup subThreadGroup2 = new ThreadGroup(threadGroup,"subThreadGroup2"); + + ThreadGroup[] threadGroups1 = new ThreadGroup[mainGroup.activeGroupCount()]; + //递归获取 + mainGroup.enumerate(threadGroups1, true); + + Stream.of(threadGroups1).forEach((tg) -> { + if (tg != null){ + System.out.println("递归获取到的线程组===>> " + tg.getName()); + } + }); + + ThreadGroup[] threadGroups2 = new ThreadGroup[mainGroup.activeGroupCount()]; + //非递归获取 + mainGroup.enumerate(threadGroups2, false); + + Stream.of(threadGroups2).forEach((tg) -> { + if (tg != null){ + System.out.println("非递归获取到的线程组===>> " + tg.getName()); + } + }); + } + + + public static void main(String[] args) throws InterruptedException { + ThreadGroup threadGroup = new ThreadGroup("threadGroup"); + System.out.println("创建并启动所有的线程===>>> " + new Date()); + IntStream.range(0, 5).forEach((i) -> { + //将线程都添加到threadGroup线程组 + new Thread(threadGroup, () -> { + while (!Thread.currentThread().isInterrupted()){ + + } + System.out.println("子线程" + Thread.currentThread().getName() + "被中断===>>> " + new Date()); + }).start(); + }); + //主线程休眠5秒 + Thread.currentThread().sleep(5000); + System.out.println("主线程中断子线程"); + //使用线程组批量停止线程 + threadGroup.interrupt(); + } +} diff --git a/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadPoolExecutorTest.java b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadPoolExecutorTest.java new file mode 100644 index 0000000..19c9039 --- /dev/null +++ b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadPoolExecutorTest.java @@ -0,0 +1,55 @@ +/** + * Copyright 2020-9999 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.binghe.consurrent.chapter02; + +import java.util.concurrent.*; + +/** + * @author binghe + * @version 1.0.0 + * @description 通过ThreadPoolExecutor类创建线程 + */ +public class ThreadPoolExecutorTest { + + private static ThreadPoolExecutor threadPool; + + static { + threadPool = new ThreadPoolExecutor(3, 3, 30, + TimeUnit.SECONDS, new ArrayBlockingQueue<>(5)); + } + + public static void main1(String[] args) { + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + threadPool.submit(() -> { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + }); + } + public static void main2(String[] args) throws ExecutionException, InterruptedException { + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + Future future = threadPool.submit(() -> { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + return Thread.currentThread().getName(); + }); + System.out.println("从子线程中获取到的数据为===>> " + future.get()); + } + + public static void main(String[] args){ + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + threadPool.execute(() -> { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + }); + } +} diff --git a/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadTest.java b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadTest.java new file mode 100644 index 0000000..62d6afb --- /dev/null +++ b/mykit-concurrent-chapter02/src/main/java/io/binghe/consurrent/chapter02/ThreadTest.java @@ -0,0 +1,41 @@ +/** + * Copyright 2020-9999 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.binghe.consurrent.chapter02; + +import org.junit.Test; + +/** + * @author binghe + * @version 1.0.0 + * @description 继承Thread创建线程 + */ +public class ThreadTest{ + + + public static void main(String[] args){ + Thread thread = new MyThreadTask(); + thread.start(); + System.out.println("主线程名称===>> " + Thread.currentThread().getName()); + } + + + private static class MyThreadTask extends Thread{ + @Override + public void run() { + System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName()); + } + } +} diff --git a/pom.xml b/pom.xml index 796626d..cf7f4c1 100644 --- a/pom.xml +++ b/pom.xml @@ -9,6 +9,14 @@ pom 1.0.0-SNAPSHOT + + + junit + junit + 4.13.2 + + + @@ -23,5 +31,6 @@ mykit-concurrent-chapter01 + mykit-concurrent-chapter02 \ No newline at end of file -- GitLab