java - MultiThread handling and stopping midway -


i'm having trouble stopping thread in middle. part of code, in stoplightthread class have problems on first if statement. supposed wait @ least 10 secs allow user press button can change light, if button pressed should stop running thread in case thread.sleep(40000). happens when press button changes light not stop thread. if press button while there still 20secs left add 20secs 10secs yellow light, making yellow 30 secs.

edit: if wondering, stoplightcanvas.x == 3 green, stoplightcanvas.x == 2 yellow, , stoplightcanvas.x == 1 red.

class stoplightcanvas extends canvas implements actionlistener {        public void actionperformed(actionevent e)     {         if (e.getsource() == cross) {             ispressed = true;             if (x == 3 && cancross)                 x = 2;              }         repaint();     }  }   class stoplightthread extends thread {     stoplightcanvas stoplightcanvas;      stoplightthread(stoplightcanvas stoplightcanvas) {         this.stoplightcanvas = stoplightcanvas;     }      public void run()      {         if (stoplightcanvas.x == 3){                thread.sleep(10000);                stoplightcanvas.cancross = true;                thread.sleep(40000);                if(stoplightcanvas.ispressed)                    stoplightthread.interrupt();            } else if (stoplightcanvas.x == 2) {                thread.sleep(10000);                } else if (stoplightcanvas.x == 1) {                thread.sleep(60000);            }        } catch (interruptedexception e){}             stoplightcanvas.togglecolor();            stoplightcanvas.repaint();         }                } } 

the way code written, thread sleeping 40 seconds; wakes , checks stoplightcanvas.ispressed , sets interrupt flag...

if want interrupt thread while it's sleeping, need interrupt thread. eventdispatchthread fine place that, can either modify current actionlistener, or create 1 it.

public void actionperformed(actionevent e)     {         ...         stoplightthread.interrupt();     } 

if don't want expose button outside stoplightcanvas, can roll own listener support in stoplightcanvas:

class stoplightcanvas extends canvas implements actioneventlistener {    public static interface stoplightlistener extends eventlistener {      public void stoplightchanged(int state);    }     // watch out, may need threadsafe depending on usage    list<actioneventlistener> mylisteners = new linkedlist<stoplightlistener>();    public void addstoplightlistener(stoplightlistener lst) {      mylisteners.add(lst);    }     public void actionperformed(actionevent e) {         if (e.getsource() == cross) {             ispressed = true;             if (x == 3 && cancross)                 x = 2;              }         repaint();          for(stoplightlistener lst: mylisteners) {            lst.stoplightchanged(x);         }    }     ... }  public class stoplightthread extends thread implements {     stoplightthread(stoplightcanvas stoplightcanvas) {         this.stoplightcanvas = stoplightcanvas;         stoplightcanvas.addstoplightlistener(this);     }      ...      @override public void stoplightchanged(int state) {         this.interrupt();     } } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -