java - Update JProgressBar from ExecutorService -


i pinging gateways using java icmp ping function. perform fast pinging using exectorservice creates threads pinging. after address pinged (or not) want update jprogressbar after pinging. have code working updates jprogressbar before job (ping thread) finished. want update jprogressbar after job finished.

private int num_threads = runtime.getruntime().availableprocessors(); executorservice exec = executors.newfixedthreadpool(num_threads); public void run() {     int jprogressbarvalue = 0;     (;gatewaykey<=gatewaykeystop;gatewaykey++){         ip="192.168."+gatewaykey+".1";        exec.submit((new pingtask(ip,gatewaykey,true,scanframerefrence,ttl)));        jprogressbarvalue=(gatewaykey/gatewaykeystop)*100;        scanframerefrence.progressbar.setvalue(jprogressbarvalue);        scanframerefrence.progressbar.repaint();     }} 

first of all, swing components may not used outside of event dispatch thread. so, code updating progress bar must enclosed inside

swingutilities.invokelater(new runnable() {     @override     public void run() {         scanframerefrence.progressbar.setvalue(value);     } }); 

now, answer question. if want update progress bar when task finishes, easier way have task update progress bar when @ end of execution.

another way use executorcompletionservice, can notified (thanks blocking queue) when each task has finished.

also, consider posting actual, compiling code, , respecting java naming conventions: variables start lower-case letter.


Comments