import java.util.concurrent.*;
/**
* 描述,构造方法传递参数,没有返回值的情况
*
*/
public class Test implements Runnable{
private static ThreadPoolExecutor threadPool
= new ThreadPoolExecutor(10, 15,60L, TimeUnit.SECONDS,new ArrayBlockingQueue(10),new ThreadPoolExecutor.DiscardPolicy());
private int num;
public void method(int num) {
for (int i = 0; i < 40; i++) {
threadPool.execute(new Test(num));
}
}
public Test(int num) {
this.num = num;
}
public Test() {
}
@Override
public void run() {
try {
Thread.currentThread().sleep(5000);
System.out.println("核心线程数" + threadPool.getCorePoolSize());
System.out.println("最大线程数" + threadPool.getMaximumPoolSize());
System.out.println("线程池数" + threadPool.getPoolSize());
System.out.println("队列任务数" + threadPool.getQueue().size());
System.out.println(">>>>>>>>>>>>>>"+num+">>>>>>>>>>>>>>>>>>>>>");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Test().method(123);
}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 描述,成员属性set方法传递参数,没有返回值
*
*/
public class Test2 implements Runnable{
private static ThreadPoolExecutor threadPool
= new ThreadPoolExecutor(10, 15,60L, TimeUnit.SECONDS,new ArrayBlockingQueue(10),new ThreadPoolExecutor.DiscardPolicy());
public void setNum(int num) {
this.num = num;
}
private int num;
public void method(int num) {
for (int i = 0; i < 16; i++) {
Test2 x = new Test2();
x.setNum(num);
threadPool.execute(x);
}
}
@Override
public void run() {
person();
}
public void person(){
try {
//Thread.currentThread().sleep(5000);
System.out.println("核心线程数" + threadPool.getCorePoolSize());
System.out.println("最大线程数" + threadPool.getMaximumPoolSize());
System.out.println("线程池数" + threadPool.getPoolSize());
System.out.println("队列任务数" + threadPool.getQueue().size());
System.out.println(">>>>>>>>>>>>>>"+num+">>>>>>>>>>>>>>>>>>>>>");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Test2().method(123);
}
}
import java.util.concurrent.*;
/**
* 描述,实现Callable接口获取返回值
*
*/
public class Test3 implements Callable {
private static ThreadPoolExecutor threadPool
= new ThreadPoolExecutor(10, 15,60L, TimeUnit.SECONDS,new ArrayBlockingQueue(10),new ThreadPoolExecutor.DiscardPolicy());
public void setNum(int num) {
this.num = num;
}
private int num;
public String method(int num){
String s = "";
Test3 x = new Test3();
x.setNum(num);
FutureTask<String> thread = new FutureTask<String>(x);
threadPool.submit(thread);
try {
s = thread.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return s;
}
@Override
public String call() throws Exception {
return person();
}
public String person(){
try {
System.out.println("核心线程数" + threadPool.getCorePoolSize());
System.out.println("最大线程数" + threadPool.getMaximumPoolSize());
System.out.println("线程池数" + threadPool.getPoolSize());
System.out.println("队列任务数" + threadPool.getQueue().size());
System.out.println(">>>>>>>>>>>>>>"+num+">>>>>>>>>>>>>>>>>>>>>");
} catch (Exception e) {
e.printStackTrace();
}
return "dsasd";
}
public static void main(String[] args) {
for (int i = 0; i < 16; i++) {
String s = new Test3().method(123);
System.out.println(s+" fgdgfdgsgfdgdgds");
}
}
}
import java.util.concurrent.*;
/**
* 描述,匿名内部类方式的获取返回值和传递参数
*
*/
public class Test4 {
private static ThreadPoolExecutor threadPool
= new ThreadPoolExecutor(10, 15,60L, TimeUnit.SECONDS,new ArrayBlockingQueue(10),new ThreadPoolExecutor.DiscardPolicy());
public String method(int num){
FutureTask<String> thread = new FutureTask<String>(new Callable<String>() {
@Override
public String call() throws Exception {
return person(num);
}
});
threadPool.submit(thread);
String s = "";
try {
s = thread.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return s;
}
public String person(int num){
try {
Thread.sleep(5000);
System.out.println("2核心线程数" + threadPool.getCorePoolSize());
System.out.println("2最大线程数" + threadPool.getMaximumPoolSize());
System.out.println("2线程池数" + threadPool.getPoolSize());
System.out.println("2队列任务数" + threadPool.getQueue().size());
System.out.println(">>>>>>>>>>>>>>"+num+">>>>>>>>>>>>>>>>>>>>>");
} catch (Exception e) {
e.printStackTrace();
}
return "dsasd";
}
public static void main(String[] args) {
for (int i = 0; i < 16; i++) {
String s = new Test4().method(123);
System.out.println(s+" fgdgfdgsgfdgdgds");
}
}
}