Faisal Bahadur
Faisal Bahadur

Reputation: 498

Error in using ThreadPoolExecutor

I have following Job that i want ThreadPoolExecutor to run.I want to print start time and end time of each job. Start times are printing but end times are not printing. I dont know what i am doing wrong please help!

import java.util.concurrent.TimeUnit;  

public class Job  implements Runnable {
private long startTime,endTime,delay;
int id;

public Job(long delay) {
    this.delay=delay;

  }
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}

public long getstartTime() {
    return startTime;
}
public void setstartTime(long startTime) {
    this.startTime = startTime;
}
public long getendTime() {
    return endTime;
}
public void setendTime(long endTime) {
    this.endTime = endTime;
}

      @Override
public void run() {

     setstartTime(System.nanoTime());

       try{
           TimeUnit.MILLISECONDS.sleep(delay);
           }catch(InterruptedException e){
            }
    setendTime(System.nanoTime());
     }


    }

Following is main class

import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class OracleExecutorService {
    ThreadPoolExecutor executor; //= (ThreadPoolExecutor) Executors.newCachedThreadPool();
    Vector vector=new Vector();

    public OracleExecutorService() {
        super();
        this.executor= (ThreadPoolExecutor) Executors.newCachedThreadPool();
        runJobs();
        displayResults();
        this.executor.shutdown();
    }

    private void displayResults() {
        // TODO Auto-generated method stub
        for(int i=0;i<vector.size();i++){
            Job job=(Job)vector.get(i); 
            System.out.println("Job id="+job.getId()+" start time="+job.getstartTime());
            System.out.println("Job id="+job.getId()+" end time="+job.getendTime());            
        }
    }

    private void runJobs() {
        // TODO Auto-generated method stub
        int count;
        for (int i = 0; i <= 5; i++) 
        {
            Job job=new Job(100); //100milliseconds
            count=i+1;
            job.setId(count);
            vector.add(job);
            executor.execute(job);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new OracleExecutorService();


    }

}

OutPut is as follows

Job id=1 start time=935938224433767

Job id=1 end time=0

Job id=2 start time=935938224477583

Job id=2 end time=0

Job id=3 start time=935938224648899

Job id=3 end time=0

Job id=4 start time=935938224696268

Job id=4 end time=0

Job id=5 start time=935938224749952

Job id=5 end time=0

Job id=6 start time=935938224796532

Job id=6 end time=0

Upvotes: 2

Views: 761

Answers (1)

vins
vins

Reputation: 15370

It is no error.

Basically before the endTime is set, or before even the job finishes, you call the displayResults.

     runJobs();
    Thread.sleep(200);// wait for the job to finish - would make the end time is set and display
    displayResults(); 

Try this. You will know what happens.

   setendTime(System.nanoTime());
   System.out.println("I am done:" + System.nanoTime());

Upvotes: 2

Related Questions