java - Thread Applet not Working -


i've been working on awhile , can't figure out why applet not working properly:

i instantiated 2 threads in applet. created 2 buttons- start, , stop, supposed change flag values end while() loops in threads. applet not responding either button. suggestions, anyone? time!

this applet...

package prodcons;  import java.applet.applet; import java.util.*; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener;  public class mytablesetting extends applet { soup s;                                             // show     soup bowl soup's alphabet pieces int bowllength = 150;                               // bowl's dimensions variables in case want change int bowlwidth = 220; int bowlx = 60; int bowly = 10; producer p1; consumer c1;    public void init(){     setsize(400,200);                                 // make applet size big enough our soup bowl     s = new soup();                                   // instantiate soup     p1 = new producer(this, s);              // declare , instantiate 1 producer thread - state of new     c1 = new consumer(this, s);              // declare , instantiate 1 consumer thread - state of new     p1.start();                                       // start producer thread     c1.start();                                       // start consumer thread      button stop = new button("stop");     stop.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             producer.producerrunning = false;             consumer.consumerrunning = false;             soup.clearbuffer();         }     });     add(stop);      button start = new button("start");     start.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             producer.producerrunning = true;             consumer.consumerrunning = true;             p1.run();             system.out.println("heyyy");             c1.run();         }     });     add(start); }  public void paint(graphics g){                        // first make bowl , spoon     int x;     int y;     g.setcolor(color.orange);     g.filloval(bowlx, bowly, bowlwidth, bowllength);  // bowl     g.setcolor(color.cyan);     g.filloval(10, 25, 40, 55);                       // spoon     g.filloval(25, 80, 8, 75);     g.setcolor(color.black);                          // black outlines dinnerware     g.drawoval(10, 25, 40, 55);     g.drawoval(25, 80, 8, 75);     g.drawoval(bowlx,bowly, bowlwidth, bowllength);     arraylist <string> contents = s.getcontents();  // contents of soup     (string each: contents){                      // individually add each alphabet piece in soup         x = bowlx + bowlwidth/4 +(int)(math.random()* (bowlwidth/2));  // put them @ random places mimic stirring         y = bowly + bowllength/4 + (int)(math.random()* (bowllength/2));         font bigfont = new font("helvetica", font.bold, 20);         g.setfont(bigfont);         g.drawstring(each, x, y);     } }    

}

and these threads:

package prodcons;  class consumer extends thread { private soup soup; private mytablesetting bowlview; static boolean consumerrunning = true;  public consumer(mytablesetting bowl, soup s) {     bowlview = bowl;                               // consumer given gui show happening     soup = s;                                      // consumer given soup--the monitor }  public void run() {     system.out.println("consuming: "+consumerrunning);     string c;     try {     while(consumerrunning) {              // stop thread when know there no more coming; here know there 10         c = soup.eat();                            // eat soup         system.out.println("ate letter: " + c);  // show happened in console         bowlview.repaint();                        // show in bowl                sleep((int)(math.random() * 3000));    // have consumer sleep little longer or never see alphabets!     }         } catch (interruptedexception e) {             this.interrupt();         }     }  } 

and this:

package prodcons;  class producer extends thread { private soup soup; private string alphabet = "abcdefghijklmnopqrstuvwxyz"; private mytablesetting bowlview; static boolean producerrunning = true;  public producer(mytablesetting bowl, soup s) {     bowlview = bowl;        // producer given gui show happening     soup = s;               // producer given soup--the monitor }  public void run() {     string c;     try {     while (producerrunning) {                           // put in 10 things stop         system.out.println("thikns producer != null");         c = string.valueof(alphabet.charat((int)(math.random() * 26)));   // randomly pick number associate alphabet letter         soup.add(c);                                            // add soup         system.out.println("added " + c + " soup.");     // show happened in console         bowlview.repaint();                                     // show in bowl                sleep((int)(math.random() * 2000));  // sleep while not fast see     }         } catch (interruptedexception e) {             this.interrupt();         }  } } 

and here "soup" class:

package prodcons;  import java.util.*;  public class soup { private static arraylist <string> buffer = new arraylist<string>();  // buffer holds in soup private int capacity = 6;   public synchronized string eat() {      //this method can accessed 1 thing @ time      while(buffer.isempty()){                                              // cannot eat if nothing there, check see if empty                                                              try {                 wait();                                                       // if so, wait until puts there             } catch (interruptedexception e) {}                               // doing temporarily allows other synchronized methods run (specifically - add)         }                                                                     // not out of while until there eat         string toreturn = buffer.get((int)(math.random() * buffer.size()));   // random alphabet in soup              buffer.remove(toreturn);                                              // remove no 1 else can eat         buffer.trimtosize();                                                  // reduce size of buffer fit how many pieces there         notifyall();                                                          // tell waiting have eaten , done         return(toreturn); }  public synchronized void add(string c) {     while (buffer.size() == capacity) {         try {             wait();         }         catch (interruptedexception e) {}     }     buffer.add(c);     notifyall(); }  public arraylist <string> getcontents() {     return buffer; }  public static void clearbuffer() {     buffer.clear(); } 

}

thank much!

you call producer , consumer directly on main ui thread event handler start button. effect have freeze entire application. that's because main ui thread everything: event handling, repainting, etc. long hold on it, there going no event handling, repainting, etc.

you should never call takes long on main ui thread.

but since you've started threads in init method, don't need call run in action listener. remove calls:

button start = new button("start");     start.addactionlistener(new actionlistener() {         public void actionperformed(actionevent e) {             producer.producerrunning = true;             consumer.consumerrunning = true;         }     }); 

also, should make flags volatile, otherwise change them 1 thread may not visible in other thread:

volatile static boolean producerrunning = true; 

(etc.)


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -