multithreading - JAVA - Why SwingWorker Thread not processing all publish strings? -


i using worker class extends swingworker , process string values in it's process method , prints them jtextarea.

so making lot of publish(some_string) calls print strings text area, but when execute worker thread - doesn't print text publish in doinbackground() method. misses lot of publish calls, , partial of words want.

but when excute thread , put breakpoint , follow step step can see print all strings publish calls.
doing it's supposed do.

why working in debug mode in normal mode doesn't?

my code:

public class worker extends swingworker<void , string> {     public worker(int int optionofwork){};      protected void doinbackground() throws exception     {         ...         publish("hellow");         publish("this test");         ...         //  lot of **publish(some_word) calls**            ...          publish("some more words");      }//doinbackground()       @override     protected void process(list<string> wordstoprint)     {         string lastwordrecieved = wordstoprint.get(wordstoprint.size()-1);         mainwindowtextarea.append(lastword);  // not printing\appending words sent here publish calls     }       @override     protected void done()     {         publish("\ndone");     }  }//worker class   

all doing in main thread making worker object , excuting it:

worker worker = new worker(); 

and start using worker.excute();

change this:

@override protected void process(list<string> wordstoprint) {     string lastwordrecieved = wordstoprint.get(wordstoprint.size()-1);     mainwindowtextarea.append(lastword);  // not printing\appending words sent here publish calls }      @override protected void done() {     publish("\ndone"); } 

to this:

@override protected void process(list<string> wordstoprint) {     (string text: wordstoprint) {         mainwindowtextarea.append(text + "\n");     } }  @override protected void done() {     publish("\ndone");     try {         get();     } catch (interruptedexception | executionexception e) {         e.printstacktrace();     } }   

the loop how process through string list passed process method. don't forget call get() on swingworker if check see if exceptions have been thrown. if still stuck, yes, create , post mcve.

you may need put thread.sleep(10) between publish(...) calls prevent calls hogging cpu.

for example:

import java.awt.borderlayout; import java.awt.event.actionevent; import java.util.list; import java.util.concurrent.executionexception; import java.util.concurrent.timeunit; import javax.swing.*;  @suppresswarnings("serial") public class swingworkerexample extends jpanel {     private jtextarea textarea = new jtextarea(30, 40);     private myworker myworker;      public swingworkerexample() {         textarea.setfocusable(false);         jscrollpane scrollpane = new jscrollpane(textarea);         scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always);          jpanel bottompanel = new jpanel();         bottompanel.add(new jbutton(new abstractaction("start worker") {              @override             public void actionperformed(actionevent e) {                 if (myworker != null && !myworker.isdone()) {                     myworker.setlooprunning(false);                 }                  myworker = new myworker();                 myworker.execute();             }         }));         bottompanel.add(new jbutton(new abstractaction("stop worker") {              @override             public void actionperformed(actionevent e) {                 if (myworker != null && !myworker.isdone()) {                     myworker.setlooprunning(false);                 }             }         }));          setlayout(new borderlayout());         add(scrollpane);         add(bottompanel, borderlayout.page_end);     }      private class myworker extends swingworker<void, string> {         private volatile boolean looprunning = true;          @override         protected void doinbackground() throws exception {             (int j = 0; j < 1000 && looprunning; j++) {                 (int = 0; < 1000 && looprunning; i++) {                     string text = string.format("my text %03d", i);                     publish(text);                     timeunit.milliseconds.sleep(10);                 }             }             return null;         }          @override         protected void process(list<string> chunks) {             (string text : chunks) {                 textarea.append(text + "\n");             }         }          @override         protected void done() {             textarea.append("done\n");              try {                 get();             } catch (interruptedexception | executionexception e) {                 e.printstacktrace();             }         }          public void setlooprunning(boolean running) {             this.looprunning = running;         }          public boolean islooprunning() {             return looprunning;         }     }      public static void main(string[] args) {         swingutilities.invokelater(() -> createandshowgui());     }      private static void createandshowgui() {         swingworkerexample mainpanel = new swingworkerexample();         jframe frame = new jframe("swingworkerexample");         frame.setdefaultcloseoperation(jframe.dispose_on_close);         frame.add(mainpanel);         frame.pack();         frame.setlocationbyplatform(true);         frame.setvisible(true);     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -