package com.jubao.mytest.utils;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.bouncycastle.crypto.prng.ThreadedSeedGenerator;
public class ConcurrentUtil {
private int nThreads = 5;
private void test() {
//此线程池不会自动释放链接,永远都是nThreads大小
// ExecutorService exserivce = Executors.newFixedThreadPool(nThreads);
// exserivce.execute(new myRunnable());
// try {
// Thread.sleep(2000);
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// //用线程池 执行5个线程工作
// for(int i=0; i<5; i++) {
// exserivce.execute(new myRunnable());
// }
// //当提交的线程都结束后,关闭线程池 exserivce.shutdownNow()提交的线程没执行完也会关闭线程池
// exserivce.shutdown();
// while(true) {
// //如果线程池中全部线程运行结束,则打印此信息
// if(exserivce.isTerminated()) {
// System.out.println(" all thread is finished! ");
// break;
// }
// }
//注意: 此线程池运用arrayBlockingQueue 队列(有界队列),当任务超出此队列,但没有到达线程池的最大值,则会创建新的连接
ThreadPoolExecutor poolReleaseAble = new ThreadPoolExecutor(1,10,0,TimeUnit.SECONDS,blockingqueue);
poolReleaseAble.execute(new myOneRunnable());
for(int j=0;j<10;j++) {
poolReleaseAble.execute(new myRunnable());
}
System.out.println("add after"+poolReleaseAble.getPoolSize());
poolReleaseAble.shutdown();
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("executor after"+poolReleaseAble.getPoolSize());
}
public static void main(String[] args) {
new ConcurrentUtil().test();
}
}
class myRunnable implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread());
}
}
class myOneRunnable implements Runnable {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (Exception e) {
}
System.out.println(Thread.currentThread());
}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.bouncycastle.crypto.prng.ThreadedSeedGenerator;
public class ConcurrentUtil {
private int nThreads = 5;
private void test() {
//此线程池不会自动释放链接,永远都是nThreads大小
// ExecutorService exserivce = Executors.newFixedThreadPool(nThreads);
// exserivce.execute(new myRunnable());
// try {
// Thread.sleep(2000);
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// //用线程池 执行5个线程工作
// for(int i=0; i<5; i++) {
// exserivce.execute(new myRunnable());
// }
// //当提交的线程都结束后,关闭线程池 exserivce.shutdownNow()提交的线程没执行完也会关闭线程池
// exserivce.shutdown();
// while(true) {
// //如果线程池中全部线程运行结束,则打印此信息
// if(exserivce.isTerminated()) {
// System.out.println(" all thread is finished! ");
// break;
// }
// }
ArrayBlockingQueue<Runnable> blockingqueue = new ArrayBlockingQueue<Runnable>(1);
/**
* 无界队列,线程池的最大值将会失效,线程池满了也不会在线程池里新建连接,只会用初始大小的链接
*/
//注意: 此线程池运用arrayBlockingQueue 队列(有界队列),当任务超出此队列,但没有到达线程池的最大值,则会创建新的连接
ThreadPoolExecutor poolReleaseAble = new ThreadPoolExecutor(1,10,0,TimeUnit.SECONDS,blockingqueue);
poolReleaseAble.execute(new myOneRunnable());
for(int j=0;j<10;j++) {
poolReleaseAble.execute(new myRunnable());
}
System.out.println("add after"+poolReleaseAble.getPoolSize());
poolReleaseAble.shutdown();
try {
Thread.sleep(2000);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("executor after"+poolReleaseAble.getPoolSize());
}
public static void main(String[] args) {
new ConcurrentUtil().test();
}
}
class myRunnable implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread());
}
}
class myOneRunnable implements Runnable {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (Exception e) {
}
System.out.println(Thread.currentThread());
}
}