本人刚刚接触java多线程,有什么地方有问题请多多指出下,谢谢!
测试用的代码如下:
public class TestThead {
static ArrayBlockingQueue<Object> abq = new ArrayBlockingQueue<Object>(300);
private static int ThreadNum = 3; //线程数
public static void main(String arg[]) throws InterruptedException {
//这里会要通过轮训不断的从数据库读取数据,没有数据的话不做任何的操作。
abq = getAbqtList(); //从数据库获取短信信息300条存入ArrayBlockingQueue
//创建3个线程的线程池
ThreadPool s = ThreadPool.getThreadPool(ThreadNum);
s.execute(new Runnable[] { new MyThread(abq),new MyThread(abq), new MyThread(abq) });
s.execute(new Runnable[] { new MyThread(abq),new MyThread(abq), new MyThread(abq) });
s.execute(new Runnable[] { new MyThread(abq),new MyThread(abq), new MyThread(abq) });
System.out.println(s);
s.destroy();// 所有线程都执行完成才 清空线程池和队列任务
System.out.println(s);
}
}
public class MyThread implements Runnable{
private static ArrayBlockingQueue<Object> abq = null; //存放短信的信息
public MyThread(ArrayBlockingQueue<Object> abq){
this.abq=abq;
}
public void run(){
Long begin = System.currentTimeMillis();
while (true){
try {
//Thread.sleep(50);
if(!abq.isEmpty()){
String msg = abq.take().toString();
out.println("提交的线程:"+Thread.currentThread().getName()+";信息:"+msg);
Thread.sleep(50);
}else{
Long end = System.currentTimeMillis()-begin;
System.out.println("耗时:"+end+"毫秒");
break;
}
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
}
/**
* 线程池类,线程管理器:创建线程,执行任务,销毁线程,获取线程基本信息
*/
public final class ThreadPool {
// 线程池中默认线程的个数为5
private static int worker_num = 5;
// 工作线程
private WorkThread[] workThrads;
// 未处理的任务
private static volatile int finished_task = 0;
// 任务队列,作为一个缓冲,List线程不安全
private List<Runnable> taskQueue = new LinkedList<Runnable>();
private static ThreadPool threadPool;
// 创建具有默认线程个数的线程池
private ThreadPool() {
this(5);
}
// 创建线程池,worker_num为线程池中工作线程的个数
private ThreadPool(int worker_num) {
ThreadPool.worker_num = worker_num;
workThrads = new WorkThread[worker_num];
for (int i = 0; i < worker_num; i++) {
workThrads[i] = new WorkThread();
workThrads[i].start();// 开启线程池中的线程
}
}
// 单态模式,获得一个默认线程个数的线程池
public static ThreadPool getThreadPool() {
return getThreadPool(ThreadPool.worker_num);
}
// 单态模式,获得一个指定线程个数的线程池,worker_num(>0)为线程池中工作线程的个数
// worker_num<=0创建默认的工作线程个数
public static ThreadPool getThreadPool(int worker_num1) {
if (worker_num1 <= 0)
worker_num1 = ThreadPool.worker_num;
if (threadPool == null)
threadPool = new ThreadPool(worker_num1);
return threadPool;
}
// 执行任务,其实只是把任务加入任务队列,什么时候执行有线程池管理器觉定
public void execute(Runnable task) {
synchronized (taskQueue) {
taskQueue.add(task);
taskQueue.notify();
}
}
// 批量执行任务,其实只是把任务加入任务队列,什么时候执行有线程池管理器觉定
public void execute(Runnable[] task) {
synchronized (taskQueue) {
for (Runnable t : task)
taskQueue.add(t);
taskQueue.notify();
}
}
// 批量执行任务,其实只是把任务加入任务队列,什么时候执行有线程池管理器觉定
public void execute(List<Runnable> task) {
synchronized (taskQueue) {
for (Runnable t : task)
taskQueue.add(t);
taskQueue.notify();
}
}
// 销毁线程池,该方法保证在所有任务都完成的情况下才销毁所有线程,否则等待任务完成才销毁
public void destroy() {
while (!taskQueue.isEmpty()) {// 如果还有任务没执行完成,就先睡会吧
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 工作线程停止工作,且置为null
for (int i = 0; i < worker_num; i++) {
workThrads[i].stopWorker();
workThrads[i] = null;
}
threadPool=null;
taskQueue.clear();// 清空任务队列
}
// 返回工作线程的个数
public int getWorkThreadNumber() {
return worker_num;
}
// 返回已完成任务的个数,这里的已完成是只出了任务队列的任务个数,可能该任务并没有实际执行完成
public int getFinishedTasknumber() {
return finished_task;
}
// 返回任务队列的长度,即还没处理的任务个数
public int getWaitTasknumber() {
return taskQueue.size();
}
// 覆盖toString方法,返回线程池信息:工作线程个数和已完成任务个数
@Override
public String toString() {
return "线程池中线程的个数:" + worker_num + " 已处理的任务:"
+ finished_task + " 队列中未处理的任务:" + getWaitTasknumber();
}
/**
* 内部类,工作线程
*/
private class WorkThread extends Thread {
// 该工作线程是否有效,用于结束该工作线程
private boolean isRunning = true;
/*
* 关键所在啊,如果任务队列不空,则取出任务执行,若任务队列空,则等待
*/
@Override
public void run() {
Runnable r = null;
while (isRunning) {// 注意,若线程无效则自然结束run方法,该线程就没用了
synchronized (taskQueue) {
while (isRunning && taskQueue.isEmpty()) {// 队列为空
try {
taskQueue.wait(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!taskQueue.isEmpty())
r = taskQueue.remove(0);// 取出任务
}
if (r != null) {
r.run();// 执行任务
}
finished_task++;
r = null;
}
}
// 停止工作,让该线程自然执行完run方法,自然结束
public void stopWorker() {
isRunning = false;
}
}
1 个解决方案
#1
java 并发编程 可以看看Executor 框架
private static final int NTHREADS = 100;
/**
* 创建固定线程数量的线程池
*/
private static final Executor exec = Executors.newFixedThreadPool(NTHREADS);
#1
java 并发编程 可以看看Executor 框架
private static final int NTHREADS = 100;
/**
* 创建固定线程数量的线程池
*/
private static final Executor exec = Executors.newFixedThreadPool(NTHREADS);