线程池学习
This commit is contained in:
parent
1f919fffba
commit
e69747d335
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -0,0 +1,11 @@
|
||||||
|
package bunny.thread.Module;
|
||||||
|
|
||||||
|
public class RunnableTest1 implements Runnable {
|
||||||
|
/**
|
||||||
|
* Runs this operation.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
System.out.println("实现 Runnable 接口");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
package bunny.thread.Module;
|
||||||
|
|
||||||
|
public class ThreadExtendTest extends Thread {
|
||||||
|
public void run() {
|
||||||
|
System.out.println("继承并运行线程。。。");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,29 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
public class ThreadTest0 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("这是开头内容。。。");
|
||||||
|
|
||||||
|
// 创建线程
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
System.out.println("使用Thread创建线程");
|
||||||
|
});
|
||||||
|
|
||||||
|
// 启动线程
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
|
||||||
|
// 创建线程,并启动线程
|
||||||
|
new Thread(() -> {
|
||||||
|
System.out.println("使用Thread创建线程2");
|
||||||
|
}).start();
|
||||||
|
|
||||||
|
System.out.println("这是最后内容");
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("---------------------代码文本分割线,不是线程分割线---------------------------");
|
||||||
|
|
||||||
|
Runnable runnable = () -> System.out.println("使用Runnable启动线程");
|
||||||
|
runnable.run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
public class ThreadTest1 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("线程学习开始。。。");
|
||||||
|
|
||||||
|
Runnable runnable = () -> {
|
||||||
|
System.out.println("线程学习 Runnable。。。");
|
||||||
|
};
|
||||||
|
|
||||||
|
System.out.println("线程结束---1。。。");
|
||||||
|
Thread thread = new Thread(runnable);
|
||||||
|
thread.start();
|
||||||
|
System.out.println("线程结束---2。。。");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
import bunny.thread.Module.RunnableTest1;
|
||||||
|
import bunny.thread.Module.ThreadExtendTest;
|
||||||
|
|
||||||
|
public class ThreadTest2 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 继承Thread类
|
||||||
|
ThreadExtendTest threadExtendTest = new ThreadExtendTest();
|
||||||
|
threadExtendTest.start();
|
||||||
|
|
||||||
|
// 实现Runnable接口
|
||||||
|
RunnableTest1 runnableTest1 = new RunnableTest1();
|
||||||
|
runnableTest1.run();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
|
public class ThreadTest3 {
|
||||||
|
private static int count = 0;
|
||||||
|
private static Integer threadCount = 0;
|
||||||
|
private static Integer lockCount = 0;
|
||||||
|
private static Integer lockReentrantLockCount = 0;
|
||||||
|
private static final Object lockObject = new Object();
|
||||||
|
private static final ReentrantLock lock = new ReentrantLock();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* * 资源竞争代码示例
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 这是单线程下没有竞争的示例
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
for (int j = 0; j < 1000; j++) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 有资源竞争,值不确定是多少,每一次运行都不一样
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
new Thread(() -> {
|
||||||
|
for (int j = 0; j < 1000; j++) {
|
||||||
|
threadCount++;
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用对象锁解决这个问题
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
new Thread(() -> {
|
||||||
|
synchronized (lockObject) {
|
||||||
|
for (int j = 0; j < 1000; j++) {
|
||||||
|
lockCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用锁解决
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
new Thread(() -> {
|
||||||
|
lock.lock();
|
||||||
|
try {
|
||||||
|
for (int j = 0; j < 1000; j++) {
|
||||||
|
lockReentrantLockCount++;
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(1000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
System.out.println("count 值:" + count);
|
||||||
|
System.out.println("threadCount 值:" + threadCount);
|
||||||
|
System.out.println("lockCount 值:" + lockCount);
|
||||||
|
System.out.println("lockReentrantLockCount 值:" + lockReentrantLockCount);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
|
||||||
|
public class ThreadTest4 {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
// 同步添加数组
|
||||||
|
ArrayList<Integer> list = new ArrayList<>();
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
for (int j = 0; j < 1000; j++) {
|
||||||
|
list.add(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 线程添加数组
|
||||||
|
ArrayList<Integer> list1 = new ArrayList<>();// 会有竞争条件
|
||||||
|
ConcurrentLinkedQueue<Integer> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();// 不会出现竞争条件
|
||||||
|
CopyOnWriteArrayList<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList<>();// 不会出现竞争条件
|
||||||
|
List<Integer> synchronizedList = Collections.synchronizedList(list);// 这个数组内容会是正常的两倍(没有竞争条件的数组),因为复制的是之前存在的数组
|
||||||
|
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
new Thread(() -> {
|
||||||
|
for (int j = 0; j < 1000; j++) {
|
||||||
|
list1.add(j);
|
||||||
|
copyOnWriteArrayList.add(j);
|
||||||
|
synchronizedList.add(j);
|
||||||
|
concurrentLinkedQueue.add(j);
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.sleep(1000);
|
||||||
|
System.out.println("list 长度:" + list.size());
|
||||||
|
System.out.println("list1 长度:" + list1.size());
|
||||||
|
System.out.println("copyOnWriteArrayList 长度:" + copyOnWriteArrayList.size());
|
||||||
|
System.out.println("synchronizedList 长度:" + synchronizedList.size());
|
||||||
|
System.out.println("concurrentLinkedQueue 长度:" + concurrentLinkedQueue.size());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
public class ThreadTest5 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ExecutorService pool1 = Executors.newFixedThreadPool(4);
|
||||||
|
pool1.execute(() -> {
|
||||||
|
|
||||||
|
});
|
||||||
|
pool1.submit(() -> {
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
pool1.shutdown();
|
||||||
|
pool1.shutdownNow();
|
||||||
|
pool1.close();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,59 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
public class ThreadTest6 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 创建一个可缓存的线程池,该线程池的线程数量可以根据任务的需求进行自动调整。
|
||||||
|
// 当有新的任务提交时,如果有空闲线程,则立即执行;如果没有空闲线程,则创建新的线程。
|
||||||
|
// 当线程空闲一段时间后,如果线程池中的线程数量超过了核心线程数(默认为0),则这些空闲线程将被终止。
|
||||||
|
try (ExecutorService pool = Executors.newCachedThreadPool()) {
|
||||||
|
// pool.submit();
|
||||||
|
// pool.execute();
|
||||||
|
// pool.awaitTermination();
|
||||||
|
// pool.invokeAll();
|
||||||
|
// pool.isShutdown();
|
||||||
|
// pool.isTerminated();
|
||||||
|
|
||||||
|
|
||||||
|
pool.shutdownNow();
|
||||||
|
// 优雅地关闭线程池。该方法会等待线程池中的所有任务执行完成后再关闭。
|
||||||
|
pool.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个固定大小的线程池,该线程池中的线程数量始终保持不变。
|
||||||
|
// 当有新的任务提交时,如果线程池中有空闲线程,则立即执行;
|
||||||
|
// 如果没有空闲线程,则任务将等待,直到有线程可用为止。
|
||||||
|
try (ExecutorService pool = Executors.newFixedThreadPool(4)) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个单线程的线程池,该线程池中只有一个工作线程。
|
||||||
|
// 所有提交的任务按照顺序执行,即使任务抛出异常也不会影响后续任务的执行。
|
||||||
|
try (ExecutorService pool = Executors.newSingleThreadExecutor()) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个固定大小的线程池,该线程池可以执行定时任务和周期性任务。
|
||||||
|
// 除了执行普通任务外,还可以使用 schedule() 和 scheduleAtFixedRate() 方法调度任务的执行。
|
||||||
|
try (ExecutorService pool = Executors.newScheduledThreadPool(4)) {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个单线程的线程池,该线程池可以执行定时任务和周期性任务。
|
||||||
|
// 与 newScheduledThreadPool() 类似,但只有一个工作线程。
|
||||||
|
try (ExecutorService pool = Executors.newSingleThreadScheduledExecutor()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// 创建一个工作窃取线程池,该线程池基于 Fork/Join 框架。
|
||||||
|
// 它根据可用处理器的数量创建并行线程来执行任务,并且可以自动处理任务的分割和合并。
|
||||||
|
try (ExecutorService pool = Executors.newWorkStealingPool()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
public class ThreadTest7 {
|
||||||
|
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||||
|
try (ExecutorService pool = Executors.newFixedThreadPool(4)) {
|
||||||
|
Runnable runnable = () -> {
|
||||||
|
System.out.println("执行的任务1");
|
||||||
|
};
|
||||||
|
|
||||||
|
pool.submit(runnable);
|
||||||
|
Future<Integer> future = pool.submit(() -> {
|
||||||
|
System.out.println("执行的任务2");
|
||||||
|
return 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
pool.execute(() -> {
|
||||||
|
System.out.println("execute 执行任务");
|
||||||
|
});
|
||||||
|
pool.execute(() -> {
|
||||||
|
System.out.println("execute 执行任务2");
|
||||||
|
});
|
||||||
|
|
||||||
|
System.out.println(future.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package bunny.thread;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
public class ThreadTest8 {
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
try (ExecutorService pool = Executors.newFixedThreadPool(4)) {
|
||||||
|
|
||||||
|
Callable<String> task1 = () -> {
|
||||||
|
Thread.sleep(2000);
|
||||||
|
return "Task 1";
|
||||||
|
};
|
||||||
|
|
||||||
|
Callable<String> task2 = () -> {
|
||||||
|
Thread.sleep(3000);
|
||||||
|
return "Task 2";
|
||||||
|
};
|
||||||
|
|
||||||
|
Callable<String> task3 = () -> {
|
||||||
|
Thread.sleep(1500);
|
||||||
|
return "Task 3";
|
||||||
|
};
|
||||||
|
|
||||||
|
List<Callable<String>> tasks = Arrays.asList(task1, task2, task3);
|
||||||
|
|
||||||
|
// 执行所有任务
|
||||||
|
List<Future<String>> results = pool.invokeAll(tasks);
|
||||||
|
results.forEach(result -> {
|
||||||
|
try {
|
||||||
|
String s = result.get();
|
||||||
|
System.out.println(s);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue