0

I have no clue what the error is, in the following code. The error is shown in this line ScheduledFuture sf=executor.scheduleWithFixedDelay(worker,2,5,TimeUnit.SECONDS); at timeunit.seconds in the following code. I have mentioned the error at the end of the code.

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
Struse
  • 75
  • 3
  • 13

0 Answers0