回 帖 发 新 帖 刷新版面

主题:一个泛型的错误

/**
 * @(#)ThreadPool.java
 *
 *
 * @author 
 * @version 1.00 2007/10/26
 */

package multithread2;
import java.util.LinkedList;
public class ThreadPool extends ThreadGroup{
private  boolean isClosed=false;
private  LinkedList <Runnable> workQueue;
private static int threadPoolID;
private int threadID;
    public ThreadPool(int poolSize) {
        super("ThreadPool-"+(threadPoolID++));
        setDaemon(true);
        workQueue=new LinkedList<Runnable>();
        for(int i=0;i<poolSize;i++){
            new WorkThread().start();
        }
    }
    public synchronized woid execute(Runnable task){
        if(isClosed){
            throw new IllegalStateException();
        }
        if(task!=null){
            workQueue.add(task);
            notify();
        }
    }
    protected synchronized  Runnable getTask()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();
        }
    }
    public void jion(){
        synchronized (this){
            isClosed=true;
            notifyAll();
        }
        Thread[]threads=new Thread[activeCount()];
        int count=enumerate(threads);
        for(int i=0;i<count;i++){
            try{
                threads[i].join();
            }
            catch(InterruptedException ex){}
        }
    }
    private class WorkThread extends Thread{
        public WorkThread(){
            super(ThreadPool.this,"WorkThread-"+(threadID++));
        }
        public void run(){
            while(!isInterrupted()){
                Runnable task=null;
                try{
                    task=getTask();
                }catch(InterruptedException ex){
                }
                if(task==null)return;
                try{
                    task.run();
                }catch(Throwable t){
                    t.printStackTrace();
                }
            }
        }
    }
}
编译的时候就出错了
--------------------配置:            <Default>--------------------
E:\java game\ThreadPool.java:13: <identifier> expected
private  LinkedList <Runnable> workQueue;
                    ^
E:\java game\ThreadPool.java:19: '(' or '[' expected
        workQueue=new LinkedList<Runnable>();
                                ^
2 errors
我用的java 1.6

回复列表 (共4个回复)

沙发

//public synchronized woid execute(Runnable task){
public synchronized void execute(Runnable task) {

这边改了就能编译成功

板凳

public synchronized woid execute(Runnable task){

void写成woid了

在我机子上编译通过(我把包去掉)

3 楼

我这么改了后,还是出现这样的错误
你用的是jdk 1.6吗

4 楼

我终于弄明白了,原来我前一阵做jsp的时候,把jdk 1.4装在了以前装1.6的文件夹里,所以误以为是1.6。jdk 1.4刚好不支持泛型,才弄出这个错误了

我来回复

您尚未登录,请登录后再回复。点此登录或注册