java - Managing thread from a mouseclick -
may basic question. i'm little confused it. in design i've thread started mouse click. method looks this:
runnable r = new runnable(){ public void run(){ a.firstmethod(); a.secondmethod(); b.firstmethod(); b.secondmethod(); b.thirdmethod(); } }; new thread(r).start();
here b.firstmethod(), b.secondmethod(), b.thirdmethod();
accessing same variables , database operations during execution (i.e. locking, reading, writing etc.). but, if mouse clicked during execution or before finishing tasks error message database. how can handle such type of situations. here cannot force user wait simple progressbar.
to handle such situation, can disable button once thread has started execution, , enable again once thread going finish execution. code following:
runnable r = new runnable() { public void run() { button.setenabled(false); a.firstmethod(); a.secondmethod(); b.firstmethod(); b.secondmethod(); b.thirdmethod(); button.setenabled(true); } }; new thread(r).start();
or when button clicked, can use boolean variable check if thread in progress or not, , decide needs done like:
volatile boolean inprogress = false; // instance variable runnable r = new runnable() { public void run() { inprogress = true; a.firstmethod(); a.secondmethod(); b.firstmethod(); b.secondmethod(); b.thirdmethod(); inprogress = false; } }; if(!inprogress) new thread(r).start(); else popup message perhaps
Comments
Post a Comment