请教各位:
我不知道为什么Java线程池实现多线程时任务必须是Runnable类型的。但在调用任务执行时并没有体现Runnable类型,只是用 对象.方法 调用。
用别的的类型有什么不好。
我觉得应该差不多。package threadPool;import java.util.LinkedList;public class ThreadPool extends ThreadGroup{
private boolean isClosed = false;//线程池是否关闭
private LinkedList<Runnable> workQueue;//表示工作队列
private static int poolID;//表示线程池ID
private int threadID;//表示工作线程ID
public ThreadPool(int poolSize){
super("ThreadPool-" +(poolID++));
setDaemon(true);
workQueue = new LinkedList<Runnable>();//创建工作队列
for (int i = 0; i < poolSize; i++) {
new WorkThread().start();
}
}
/**
* 向工作队列中加入一个新任务,由工作线程去执行
*/
public synchronized void execute(Runnable task){
if(isClosed){//线程池已关闭则抛出IllegalStateException异常。
throw new IllegalStateException();
}
if(task != null){
workQueue.add(task);
notify();//唤醒正在getTask()方法中等待任务的线程
}
}
/**
* 重工作队列中取出一个任务,工作线程会调用此方法
*/
protected synchronized Runnable geTask() throws InterruptedException{
while(workQueue.size() == 0){
if(isClosed) return null;
wait();
}
return workQueue.removeFirst();
}
/**
* 关闭线程池
*/
public synchronized void close(){
if(!isClosed){
isClosed = true;
workQueue.clear();//清空工作队列
interrupt();//中断所有的工作线程,该方法继承于ThreadGroup类
}
}
/**
* 等待工作线程将所有的任务完成
*/
public void join (){
synchronized(this){
isClosed = true;
notifyAll();
}
Thread[] thread = new Thread[activeCount()];
int count = enumerate(thread);//继承与类ThreadGroup,获得线程中所有的工作线程
for (int i = 0; i < count; i++) {
try {
thread[i].join();
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
/**
* 内部类:工作线程
* @author Administrator
*
*/
private class WorkThread extends Thread{
private WorkThread(){
//加入到当前的ThreadPool中
super(ThreadPool.this,"WorkThread-"+(threadID++));
}
public void run() {
Runnable task = null;
try {
task = geTask();//取出任务
} catch (InterruptedException ex) {
ex.printStackTrace();
}
//如果getTask()返回是null或者执行etTask()被中断。则结束此线程
if(task == null) return;
try {
task.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}}
我不知道为什么Java线程池实现多线程时任务必须是Runnable类型的。但在调用任务执行时并没有体现Runnable类型,只是用 对象.方法 调用。
用别的的类型有什么不好。
我觉得应该差不多。package threadPool;import java.util.LinkedList;public class ThreadPool extends ThreadGroup{
private boolean isClosed = false;//线程池是否关闭
private LinkedList<Runnable> workQueue;//表示工作队列
private static int poolID;//表示线程池ID
private int threadID;//表示工作线程ID
public ThreadPool(int poolSize){
super("ThreadPool-" +(poolID++));
setDaemon(true);
workQueue = new LinkedList<Runnable>();//创建工作队列
for (int i = 0; i < poolSize; i++) {
new WorkThread().start();
}
}
/**
* 向工作队列中加入一个新任务,由工作线程去执行
*/
public synchronized void execute(Runnable task){
if(isClosed){//线程池已关闭则抛出IllegalStateException异常。
throw new IllegalStateException();
}
if(task != null){
workQueue.add(task);
notify();//唤醒正在getTask()方法中等待任务的线程
}
}
/**
* 重工作队列中取出一个任务,工作线程会调用此方法
*/
protected synchronized Runnable geTask() throws InterruptedException{
while(workQueue.size() == 0){
if(isClosed) return null;
wait();
}
return workQueue.removeFirst();
}
/**
* 关闭线程池
*/
public synchronized void close(){
if(!isClosed){
isClosed = true;
workQueue.clear();//清空工作队列
interrupt();//中断所有的工作线程,该方法继承于ThreadGroup类
}
}
/**
* 等待工作线程将所有的任务完成
*/
public void join (){
synchronized(this){
isClosed = true;
notifyAll();
}
Thread[] thread = new Thread[activeCount()];
int count = enumerate(thread);//继承与类ThreadGroup,获得线程中所有的工作线程
for (int i = 0; i < count; i++) {
try {
thread[i].join();
} catch (InterruptedException e) {
// TODO: handle exception
}
}
}
/**
* 内部类:工作线程
* @author Administrator
*
*/
private class WorkThread extends Thread{
private WorkThread(){
//加入到当前的ThreadPool中
super(ThreadPool.this,"WorkThread-"+(threadID++));
}
public void run() {
Runnable task = null;
try {
task = geTask();//取出任务
} catch (InterruptedException ex) {
ex.printStackTrace();
}
//如果getTask()返回是null或者执行etTask()被中断。则结束此线程
if(task == null) return;
try {
task.run();
} catch (Exception e) {
e.printStackTrace();
}
}
}}
对象.方法,就是为了调用而已。