java - Increacing the speed of thread -
after every call of thread it's speed increased (specifically - firstcirclerepaintthread, secoundcirclerepaintthread, linerepaintthread). main thread's speed normal. i've read this question, thread's speed always increasing. github, jar on dropbox p.s. sorry bad english
the problem is, each time unblock
creating new instance of threads, you're not stopping old ones, still running, updating ui
public void block() { setvisible(true); // create bunch of new threads... firstcirclethr = new firstcirclerepaintthread(); firstcirclethr.setpriority(thread.max_priority); firstcirclethr.start(); secoundcirclethr = new secoundcirclerepaintthread(); secoundcirclethr.setpriority(thread.max_priority); secoundcirclethr.start(); linethr = new linerepaintthread(); linethr.setpriority(thread.max_priority); linethr.start(); } public void unblock(){ setvisible(false); // dereference old ones, still running... firstcirclethr = null; secoundcirclethr = null; linethr = null; system.out.println("yeah! off it!"); }
for example...
public class firstcirclerepaintthread extends thread{ public static final long speed_of_rotating = 25l; @override public void run(){ //while(true){ maincycle.frame.panel.startanglefirst = 34; int = 0; random r = new random(); // infinity , beyond... while(true){
you need supply way can stop these threads...without calling stop
...
for example...
public class firstcirclerepaintthread extends thread{ private volatile boolean keeprunning = true; public static final long speed_of_rotating = 25l; public void kull() { keeprunning = false; interrupt(); try { join(); } catch (interruptedexception ex) { } } @override public void run(){ //while(true){ maincycle.frame.panel.startanglefirst = 34; int = 0; random r = new random(); while(keeprunning){
now in block
method, should calling firstcirclethr.kull()
, stop thread
before returning...
you have seen if used debugger or left frame visible between cycles...
now, having said that, you're violating single thread rules swing, updating state of ui every thread except event dispatching thread.
take @ concurrency in swing , consider use of swingworker
or swing timer
.
you should consider maintaining few background processes possible in order maintain performance , reduce complexity of changing model , interafce
Comments
Post a Comment