java - SwingWorker creates multiple thread -
i don't know if used swingworker correctly. in requirements, need refreshed every seconds, need swingworker needs execute every seconds.
a part of code illustrated below on how used loop swingworker.
// parent class /** * initialize background services. */ private void initservices() { this.dtmodel = (defaulttablemodel) tblhistory.getmodel(); // default data model of jtable this.updatetimer = new timer(1000, new actionlistener() { // don't know if right, use timer loop swingworker @override public void actionperformed(actionevent e) { new callhistorywatcherservice(callhistory.this, updatetimer, dtmodel).execute(); } }); updatetimer.setrepeats(false); updatetimer.start(); } // watcher class /** * watch callinformations if update has been made. * check every seconds. * @param history * @param updatetimer * @param dtmodel */ public callhistorywatcherservice(callhistory history, javax.swing.timer updatetimer, defaulttablemodel dtmodel) { // initialize values } @override protected collection<vector> doinbackground() throws exception { collection<vector> chunks = new arraylist<>(); (callinformations info : callinformations.getcallinformations(orderby.ascending)) { vector v = new vector(); // data database place vector chunks.add(v); } return chunks; } @override protected void done() { try { history.updatetabledata(get()); // parent class, manually update data based on new collection of data system.out.println("call history refreshed..."); } catch (executionexception | interruptedexception e) { e.printstacktrace(); } column_names.clear(); updatetimer.restart(); // again, don't know if right, restart timer execute swingworker }
as stated in javadoc of swingworker:
there 3 threads involved in life cycle of swingworker:
- current thread
- worker thread
- event dispatch thread
the background job done on single worker thread. internal implementation of swingworker
uses shared pool of threads via executorservice
run background jobs of multiple swingworkers
. quoting javadoc of swingworker.execute()
:
schedules swingworker execution on worker thread. there number of worker threads available.
so see normal threads of thread pool used swingworker
internally.
edit:
in case wouldn't use swingworker
. use separate thread (not edt) work, , once it's done, update ui in edt call of swingutilities.invokelater()
. , use repeatable timer
perform job periodically.
Comments
Post a Comment