java - Writing to JTextArea with CachedThreadPool -


i asked in codereview closed.

for school assignment have create 54 threads run simultaneously executors.newcachedthreadpool() write jtextarea in thread safe manner. each thread has write 'a' through 'z' field 1000 times. i've been having issues making program thread safe no matter put synchronized keyword. have runnable class writing i'm having issues keeping thread safe. method tried works putting thread.sleep(500) in loop cycles through letters, doesn't work when increase iterations , giving warning in netbeans anyway.

will putting synchronized keyword in correct place make in thread safe or have change thread itself? i'd left of previous attempts in comments below.

i changed code works once. runs 1 instance writes jtextarea i'm having issues turning multiple synchronized threads.

alphabetthread.java

package threadpool;  import javax.swing.jtextarea;  public class alphabetthread implements runnable{      char letter;     jtextarea toedit;     //final int number_of_iterations = 1000;      public alphabetthread(char e, jtextarea tf) {         letter = e;         toedit = tf;     }      @override     public void run() {         //for (int = 0; < number_of_iterations; i++) {              toedit.settext(toedit.gettext() + letter);      }  //    public synchronized void createthread() { //        (int = 0; < number_of_iterations; i++) { // //            toedit.settext(toedit.gettext() + letter); //        } //    } } 

threadpool.java

package threadpool;  import java.util.concurrent.executorservice; import java.util.concurrent.executors;  public class threadpool {  //    static char letter; // //    thread alphabetthread = new thread(() -> { //        char e = letter; //        mainwindow.textbox.settext(mainwindow.textbox.gettext() + e); //    }); //    public static synchronized alphabetthread createthread(char e, jtextarea tf) throws interruptedexception { //        alphabetthread runme = new alphabetthread(e, tf); //        return runme; //    }     public static void main(string[] args) {         textwindow mainwindow = new textwindow();         executorservice pool = executors.newcachedthreadpool();          (char alphabet = 'a'; alphabet <= 'z'; alphabet++) {              alphabetthread alphabetthread = new alphabetthread(alphabet, mainwindow.textbox);             alphabetthread.run();          }         pool.shutdown();     } } 

textwindow.java

package threadpool;  import java.awt.dimension; import java.awt.headlessexception; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextarea;  public class textwindow extends jframe {      public jpanel mainpanel = new jpanel();     public jtextarea textbox = new jtextarea();      public textwindow() throws headlessexception {          add(mainpanel);         textbox.setpreferredsize(new dimension(450,450));         textbox.setvisible(true);         mainpanel.add(textbox);         setvisible(true);         setsize(500, 500);         setdefaultcloseoperation(jframe.exit_on_close);         setlocationrelativeto(null);         pack();     }  } 

a full solution not necessary (though nice). prefer pointers on correct code.

there couple of issues code. firstly, general rule need synchronize using same monitor object threads. attempts seem @ synchronizing creating threads, rather when doing writing text area. if swing thread safe, use text area monitor object, example:

synchronized (toedit) {     toedit.settext(toedit.gettext() + letter); } 

and enough synchronize writes. however, swing not thread safe. second issue. modifying jcomponents must done in event dispatch thread. done using invokelater() or (rarely)invokeandwait():

swingutilities.invokelater(new runnable() {     @override     public void run() {         toedit.settext(toedit.gettext() + letter);     } } 

this means write requests queued, , won't need worry more thread safety of writing part.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -