提交 153d221f 编写于 作者: 冰 河's avatar 冰 河

提交第2章的代码

上级 8727363e
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mykit-concurrent-jdk</artifactId>
<groupId>io.binghe.concurrent</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mykit-concurrent-chapter02</artifactId>
</project>
\ No newline at end of file
/**
* Copyright 2020-9999 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String>{
@Override
public String call() throws Exception {
System.out.println("新创建的线程名称===>> " + Thread.currentThread().getName());
return Thread.currentThread().getName();
}
}
/**
* 以匿名类的方式创建线程
*/
public FutureTask createThreadByCallableAnonymousClass() {
return new FutureTask<>(new Callable<String>() {
@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());
}
}
/**
* Copyright 2020-9999 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String> 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());
});
}
}
/**
* Copyright 2020-9999 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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());
}
}
/**
* Copyright 2020-9999 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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();
}
}
/**
* Copyright 2020-9999 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<String> 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());
});
}
}
/**
* Copyright 2020-9999 the original author or authors.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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());
}
}
}
......@@ -9,6 +9,14 @@
<packaging>pom</packaging>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
......@@ -23,5 +31,6 @@
</build>
<modules>
<module>mykit-concurrent-chapter01</module>
<module>mykit-concurrent-chapter02</module>
</modules>
</project>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册