我不知道错误是什么,在下面的代码中。错误显示在此行中 ScheduledFuture sf = executor.scheduleWithFixedDelay(worker,2,5,TimeUnit.SECONDS);在timeunit.seconds 在以下代码中。我在代码末尾提到了错误。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.io.*;
class WorkerThread implements Runnable {
private String command;
public WorkerThread(String s){
this.command=s;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
processCommand();
System.out.println(Thread.currentThread().getName()+" End.");
}
private void processCommand() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString(){
return this.command;
}
}
public class ThreadPool {
public static ScheduledExecutorService executor = Executors.newScheduledThreadPool(2);
public static void main(String[] args)
{
WorkerThread worker = new WorkerThread("proud");
int i=0;
while(i<2)
{
ScheduledFuture<?> sf=executor.scheduleWithFixedDelay(worker,2,5,TimeUnit.SECONDS);
i++;
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}
ThreadPool.java:50: error: cannot find symbol
ScheduledFuture<?> sf=executor.scheduleWithFixedDelay(worker,2,5,TimeUnit.SECONDS);
^
symbol: class ScheduledFuture
location: class ThreadPool
1 error