java - Program locks up when attempting to use threads -
this first post here it's okay-like.
i'm in netbeans. i've made window buttons , such.
i have class called simplethread looks this:
public class simplethread extends thread { public simplethread() { } @override public void run() { }
and have different kinds of subclass threads extend simplethread (timerthread, mousegrabthread).
public class mousegrabthread extends simplethread{ private point pt; private jtextfield x, y; public mousegrabthread(jtextfield x, jtextfield y) { super(); this.x = x; this.y = y; } @override public void run() { while(this.isalive()) { int[] xyval = new int[2]; xyval = getcoords(); x.settext("" + xyval[0]); y.settext("" + xyval[1]); } } public int[] getcoords() { point pt = mouseinfo.getpointerinfo().getlocation(); int[] retarr = new int[2]; retarr[0] = (int)pt.getx(); retarr[1] = (int)pt.gety(); return retarr; } public class timerthread extends simplethread { private jtextarea label; private int time; public timerthread(jtextarea label, int time) { super(); this.label = label; this.time = time; } @override public void run() { int counter = time; while(counter != -1) { label.settext("you have: " + counter + " seconds until timer ends!\n"); counter--; try { this.sleep(1000); } catch (interruptedexception ex) { system.out.println("thread interrupted"); } } stop(); }
in ui window class, have this:
private void jbutton2mouseclicked(java.awt.event.mouseevent evt) { simplethread timer = new timerthread(jtextarea1, 10); //this counts down 10 seconds , updates status text box each second simplethread mousegrab = new mousegrabthread(jtextfield1, jtextfield2); //this grabs mouse coords , updates 2 boxes in ui. timer.start(); if(timer.isalive()) { mousegrab.start(); } while(timer.isalive())//###### { if(!mousegrab.isalive()) { mousegrab.start(); } }
}
the program freezes 10 seconds when press button.
i guess line i've marked (//#####) 1 thats causing ui freeze duration of timer cause it's running in main thread. i'm not sure how correct this.
please excuse lack of knowledge of programming, i'm getting threads on own while i'm taking simple course in university data structures. if possible, can "dumb" answer down as possible?
also, know i'm terrible doing this, call stop() function though it's not nice (don't shoot me it, don't know how else it!) if can answer me on how nicely, great!
what might want ending mousegrab
@ end of countdown:
private void jbutton2mouseclicked(java.awt.event.mouseevent evt) { simplethread timer = new timerthread(jtextarea1, 10); //this counts down 10 seconds , updates status text box each second simplethread mousegrab = new mousegrabthread(jtextfield1, jtextfield2); //this grabs mouse coords , updates 2 boxes in ui. mousegrab.start(); timer.start(); // wait until countdown finishes while(timer.isalive()) {} mousegrab.stop(); }
in code pasted, kept starting mousegrab
while timer
running. might rather want let mouse grabbing running while timer on.
edit : indeed, stop()
deprecated, should use boolean running
attribute in timerthread
, wrapping content of run()
method in some
while (running) { /* stuff */ }
and "stop" thread externally setter. correct answer example:
mousegrab.start(); timer.start(); // wait until countdown finishes while(timer.isalive()) {} mousegrab.setrunning(false); }
edit2 : seems want:
private void jbutton2mouseclicked(java.awt.event.mouseevent evt) { simplethread mousegrab = new mousegrabthread(jtextfield1, jtextfield2); //this grabs mouse coords , updates 2 boxes in ui. simplethread timer = new timerthread(mousegrab, jtextarea1, 10); //this counts down 10 seconds , updates status text box each second mousegrab.start(); timer.start(); }
with:
public class mousegrabthread extends simplethread { private point pt; private jtextfield x, y; private boolean running; public mousegrabthread(jtextfield x, jtextfield y) { super(); this.x = x; this.y = y; } @override public void run() { running = true; while(running) { int[] xyval = new int[2]; xyval = getcoords(); x.settext("" + xyval[0]); y.settext("" + xyval[1]); } } public int[] getcoords() { point pt = mouseinfo.getpointerinfo().getlocation(); int[] retarr = new int[2]; retarr[0] = (int)pt.getx(); retarr[1] = (int)pt.gety(); return retarr; } public void break() { this.running = false; } } // ------------- // public class timerthread extends simplethread { private mousegrabthread mousegrab; private jtextarea label; private int time; public timerthread(mousegrabthread mousegrab, jtextarea label, int time) { super(); this.label = label; this.time = time; this.mousegrab = mousegrab; } @override public void run() { int counter = time; while(counter != -1) { label.settext("you have: " + counter + " seconds until timer ends!\n"); counter--; try { this.sleep(1000); } catch (interruptedexception ex) { system.out.println("thread interrupted"); } } mousegrab.break(); } }
Comments
Post a Comment