Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added TestProject/Agrona-0.4.13.jar
Binary file not shown.
Binary file added TestProject/agrona-agent-2.3.2.jar
Binary file not shown.
Binary file added TestProject/arcCommon-0.16.jar
Binary file not shown.
Binary file added TestProject/awaitility-4.2.0.jar
Binary file not shown.
Binary file added TestProject/awaitility-test-support-3.1.6.jar
Binary file not shown.
Binary file added TestProject/disruptor-2.10.4.jar
Binary file not shown.
Binary file added TestProject/hippo4j-monitor-base-1.5.0.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added TestProject/litesockets-http-protocol-0.27.jar
Binary file not shown.
Binary file added TestProject/log4j-core-2.17.1.jar
Binary file not shown.
Binary file added TestProject/netty-common-4.1.77.Final.jar
Binary file not shown.
Binary file added TestProject/rxjava-core-0.19.6.jar
Binary file not shown.
Binary file added TestProject/rxjava-math-1.0.0.jar
Binary file not shown.
Binary file added TestProject/shenyu-disruptor-2.6.0.jar
Binary file not shown.
Binary file added TestProject/taskflow-core-1.0.0-SNAPSHOT.jar
Binary file not shown.
Binary file not shown.
Binary file added TestProject/websocket-jetty-server-11.0.15.jar
Binary file not shown.
Binary file added TestProject/xorcery-disruptor-0.10.1.jar
Binary file not shown.
40 changes: 40 additions & 0 deletions src/Mytest/CoreMax_ThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CoreMax_ThreadPool {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
Integer.MAX_VALUE,
10, // 直接使用 Integer.MAX_VALUE
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());

threadPoolExecutor.execute(() -> {
while(!threadPoolExecutor.isShutdown()) {
try {
Thread.sleep(1000);
System.out.println("This is CoreMax_ThreadPool...");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});

// 主线程做
System.out.println("Main Thread is to Set_Max_Value...");
// threadPoolExecutor.setMaximumPoolSize(Integer.MAX_VALUE);

try {
Thread.sleep(3000);
threadPoolExecutor.shutdown();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

}
}
39 changes: 39 additions & 0 deletions src/Mytest/CoreMax_ThreadPool2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CoreMax_ThreadPool2 { public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
10, // 直接使用 Integer.MAX_VALUE
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());

threadPoolExecutor.execute(() -> {
while(!threadPoolExecutor.isShutdown()) {
try {
Thread.sleep(1000);
System.out.println("This is CoreMax_ThreadPool...");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});

// 主线程做
System.out.println("Main Thread is to Set_Max_Value...");
threadPoolExecutor.setCorePoolSize(Integer.MAX_VALUE);

try {
Thread.sleep(3000);
threadPoolExecutor.shutdown();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

}
}
35 changes: 35 additions & 0 deletions src/Mytest/DPSETest_ThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class DPSETest_ThreadPool {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
10,
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());

threadPoolExecutor.execute(() -> {
while(!threadPoolExecutor.isShutdown()) {
try {
Thread.sleep(1000);
System.out.println("This is threadPoolExecutor...");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
// 主线程做
System.out.println("Main Thread is to SetRejectedExecutionHandler...");
threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());

threadPoolExecutor.shutdown();
}
}
25 changes: 25 additions & 0 deletions src/Mytest/Excp_ThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Excp_ThreadPool {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
10, // 直接使用 Integer.MAX_VALUE
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());

threadPoolExecutor.submit(()->{
int result= 10/0;
// 异常未处理
// System.out.println("This is Excp_ThreadPool");
});

threadPoolExecutor.shutdown();
}
}
31 changes: 31 additions & 0 deletions src/Mytest/ILTest_ThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// IL存在误报!!!还是指针分析的问题;
//这个测试用例中,有shutdown,没有shutdown
public class ILTest_ThreadPool {
public static void main(String[] args) {
// 构建线程池对象,使用 execute() 提交 Runnable,submit() 提交 Callable
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
10,
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());

threadPoolExecutor.execute(()->{
// ILChecker会检测到这种情况:
while (!threadPoolExecutor.isShutdown()) { // 触发检测
System.out.println("Running..,");
}
});

// threadPoolExecutor.shutdown();
}
}

42 changes: 42 additions & 0 deletions src/Mytest/IntMax_ThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

// 这里测试——直接使用intmax的情况
public class IntMax_ThreadPool {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
Integer.MAX_VALUE, // 直接使用 Integer.MAX_VALUE
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());

threadPoolExecutor.execute(() -> {
while(!threadPoolExecutor.isShutdown()) {
try {
Thread.sleep(1000);
System.out.println("This is IntMax_ThreadPool...");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});

// 主线程做
// System.out.println("Main Thread is to Set_Max_Value...");
// threadPoolExecutor.setMaximumPoolSize(Integer.MAX_VALUE);
try {
Thread.sleep(3000);
threadPoolExecutor.shutdown();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}


}

}
43 changes: 43 additions & 0 deletions src/Mytest/IntMax_ThreadPool2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Mytest;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

//这里测试用set方法赋值intmax的情况
public class IntMax_ThreadPool2 {
public static void main(String[] args) {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
10, // 直接使用 Integer.MAX_VALUE
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());

threadPoolExecutor.execute(() -> {
while(!threadPoolExecutor.isShutdown()) {
try {
Thread.sleep(1000);
System.out.println("This is IntMax_ThreadPool...");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});

// 主线程做
System.out.println("Main Thread is to Set_Max_Value...");
// 这个set表达式,是属于Value形式;
threadPoolExecutor.setMaximumPoolSize(Integer.MAX_VALUE);

try {
Thread.sleep(3000);
threadPoolExecutor.shutdown();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

}
}
Binary file added src/Mytest/Mytest/ILTest.class
Binary file not shown.
Binary file added src/Mytest/Mytest/ILThread.class
Binary file not shown.
50 changes: 50 additions & 0 deletions src/Mytest/RepeatedCreate_ThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class RepeatedCreate_ThreadPool {
public static void main(String[] args) {

ThreadPoolUtils threadPoolUtils = new ThreadPoolUtils();
ThreadPoolExecutor myPool = threadPoolUtils.createMyPool();
myPool.execute(()->{
System.out.println("mypool is running");
});
myPool.shutdown();
ThreadPoolExecutor myPool1 = threadPoolUtils.createMyPool();
myPool1.execute(()->{
System.out.println("mypool1 is running");
});
myPool1.shutdown();


// 循环误用模式检测!
// int i=0;
// while (i<10){
// ThreadPoolExecutor myPool = ThreadPoolUtils.createMyPool();
// myPool.execute(()->{
// System.out.println("running...");
// });
// i++;
// myPool.shutdown();
//
// }

}

}

class ThreadPoolUtils {
// 这是一个创建点 (InitPoint)
public static ThreadPoolExecutor createMyPool() {
return new ThreadPoolExecutor(
5,
10, // 直接使用 Integer.MAX_VALUE
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());
}
}
33 changes: 33 additions & 0 deletions src/Mytest/SetName_ThreadPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

//静态分析时机问题:您的检测器进行的是静态代码分析,它只能分析代码的结构和调用关系
//无法知道运行时execute()方法内部的setName()调用。
//不要再线程运行的时候设置 execute
public class SetName_ThreadPool {
public static void main(String[] args) {

ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
10, // 直接使用 Integer.MAX_VALUE
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
new ThreadPoolExecutor.AbortPolicy());
threadPoolExecutor.execute(() -> {
try {
Thread.sleep(1000);
System.out.println("This is SetNameThreadPool");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

});
threadPoolExecutor.shutdown();
}
}
49 changes: 49 additions & 0 deletions src/Mytest/SetName_ThreadPool2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package Mytest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

// 创建一个自定义的 ThreadFactory 来设置线程名称
public class SetName_ThreadPool2 {
public static void main(String[] args) {

// 创建一个自定义的线程工厂,设置线程名称
ThreadFactory namedThreadFactory = new ThreadFactory() {
private final AtomicInteger threadCount = new AtomicInteger(1);

@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setName("Qyc-thread-" + threadCount.getAndIncrement());
return thread;
}
};

// 使用自定义线程工厂创建线程池
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
5,
10, // 最大线程数
60,
TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100),
namedThreadFactory, // 设置线程工厂
new ThreadPoolExecutor.AbortPolicy()
);

// 提交任务
threadPoolExecutor.execute(() -> {
try {
Thread.currentThread().setName("qyc-thread1");
Thread.sleep(1000);
System.out.println("This is SetNameThreadPool");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});

threadPoolExecutor.shutdown();
}
}
Loading