[java] Thread error(or lack of error), not doing restarting.

Started by
3 comments, last by CodyVa 23 years, 7 months ago
I don''t know what i''m doing wrong!!! The only thing i can think of is when stop is called..somehow it''s called twice? which makes the thread not start the next time start() is called. The Problem is when i put my mouse in the window it starts running the thread okay. when i take out the mouse it stops the thread as it supposed to... but when i put the mouse back in again it doenst'' restart the thread? any suggestions? (any coding tips will be helpfull to) Here''s the relevant code i think. Public boolean mouseEnter(Event evt, int x, int y) { try { m_tthread.start(); //the thread is member of the main class } //as in, m-tthread = new Thread(this); catch (IllegalThreadStateException itse) { System.out.println(itse.toString());//don''t know if this } //works it never //showed up return true; //so it returns and no other calls are made right //even if it didn''t handle it correctly Public boolean mouseExit(Event evt, int x, int y) { if (m_tthread.isAlive()) { m_tthread.stop(); } return true; } Public void run() { while (true) { //TimeIn is just an object member in the main class. TimeIn = Long.toString(System.currentTimeMillis()); try { repaint(); Thread.sleep(250); } catch (InterruptedException ie) { m_tthread.stop(); } } }
Advertisement
My suggestion would be to put some diagnostic print statements into your mouse handler methods. It could be that one or both of them is being called more often or at different times than you expect.
I am a Jedi, like my father before me
Thread.stop() is deprecated, don''t use it. Instead of
while(true) {
...
}
in your run method do a
while(t_running) {
...
}

then instead of Thread.stop() you just set the boolean t_running = false. When you want to start the thread running again set t_running=true then call Thread.run();
my class has "implements runnable" in it...
and the thread is
m_tthread = new Thread(this);
right???
where do i get t_running from?


The thread stops when the loop ends right?


so me setting a flag doens''t actually start the thread..it just allows the loop to happen right?

Well, if I understand what you're asking properly, the problem is this: You can't start a thread that's already been stopped.

Hmm... I'm not explaining well. Once a thread has been run with its start() method, it can't be started again. You have two options: Either create a new thread each time you want to run it, or set some sort of pause() and unpause() methods in your class.

The second is probably preferable, as far as system resources go. It shouldn't be too much trouble to rig up something with wait(), notify(), and some flag. Maybe something like this:
    private boolean paused = false;run() {  //start your while loop here, then follow immediately  //with this block:    if (paused) {      synchronized (this) {        try { wait () } catch (InterruptedException e) {}      }//synchronized    }//if    //the rest of the while loop}//run()private synchronized void pause() {  paused = true;}//pause()private synchronized void unpause() {  paused = false;  notify();}//unpause    


I think that would do the job you want, without the hassle of creating a new thread every time you want to restart the animation.

(Edited: Oops, forgot to synchronize the wait() call...)


Edited by - c_wraith on September 19, 2000 8:52:40 PM

This topic is closed to new replies.

Advertisement