multithreading - Thread continuing even though it's declared null JAVA -


this question has answer here:

maybe can me- in applet declared instances of 2 threads, started them, , made "stop" button declares them null. in thread classes, made while (this != null), , threads still running after hit button.

here init method of applet:

public void init(){     system.out.println("hi");     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) {             p1 = null;             c1 = null;             s.getcontents().clear();         }     });     add(stop); } 

and here run methods of threads:

public void run() {     string c;     try {         while (this != null) {                           // 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 this:

  public void run() {     system.out.println(soup.shouldconsume);     string c;     try {         while(this != null) {              // 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();     } 

}

any ideas why not working? input appreciated! everyone!

while (this != null) can never false.

setting reference thread null neither stops thread nor causes this become null.

your code doesn't make sense.

[the java magazines of late 1990s full of while (thread.currentthread() == this) tests. made no sense either.]


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -