主题:一个泛型的错误
/**
* @(#)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
* @(#)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