java - Error in using ThreadPoolExecutor -
i have following job want threadpoolexecutor run.i want print start time , end time of each job. start times printing end times not printing. dont know 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 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; (int = 0; <= 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 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
it no error.
basically before endtime set, or before job finishes, call displayresults.
runjobs(); thread.sleep(200);// wait job finish - make end time set , display displayresults();
try this. know happens.
setendtime(system.nanotime()); system.out.println("i done:" + system.nanotime());
Comments
Post a Comment