All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net_io.utils.thread.MyThreadPool Maven / Gradle / Ivy

The newest version!
package net_io.utils.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;

import net_io.utils.thread.RunnerThread.ThreadInfo;

public class MyThreadPool {
	private static final int DEFAULT_MIN_POOL_SIZE = 8;
	private static final int DEFAULT_MAX_POOL_SIZE = 256;
	private RunnerThread[] threads = null;
	private int minPoolSize;
	private int maxPoolSize;
	private ConcurrentLinkedQueue taskQueue = new ConcurrentLinkedQueue();
	private AtomicLong rebuildThreadCount = new AtomicLong(0);
	private String threadName = null;
	
	public MyThreadPool() {
		init(DEFAULT_MIN_POOL_SIZE, DEFAULT_MAX_POOL_SIZE, 60*1000);
	}
	public MyThreadPool(int minPoolSize, int maxPoolSize) {
		init(minPoolSize, maxPoolSize, 60*1000);
	}
	public MyThreadPool(int minPoolSize, int maxPoolSize, long keepAliveTime) {
		init(minPoolSize, maxPoolSize, keepAliveTime);
	}
	public MyThreadPool(String threadName, int minPoolSize, int maxPoolSize) {
		this.threadName = threadName;
		init(minPoolSize, maxPoolSize, 60*1000);
	}
	public MyThreadPool(String threadName, int minPoolSize, int maxPoolSize, long keepAliveTime) {
		this.threadName = threadName;
		init(minPoolSize, maxPoolSize, keepAliveTime);
	}
	private void init(int minPoolSize, int maxPoolSize, long keepAliveTime) {
		if(minPoolSize < 1) {
			minPoolSize = 0;
		}
		this.minPoolSize = minPoolSize;
		this.maxPoolSize = maxPoolSize;
		if(this.maxPoolSize < this.minPoolSize) {
			this.maxPoolSize = this.minPoolSize;
		}
		//创建线程对象
		threads = new RunnerThread[this.maxPoolSize];
		for(int i=0; i= taskSize) {
					return assignCount;
				}
			}
		}
		for(i=middlePos-1; i>=0; i--) {
			if(threads[i].assignFree()) {
				assignCount++;
				if(assignCount >= taskSize) {
					return assignCount;
				}
			}
		}
		for(i=minPoolSize; i= taskSize) {
					return assignCount;
				}
			}
		}
		return assignCount;
	}
	
	public List listThreadInfo() {
		List list = new ArrayList();
		for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy