Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Interrupt a sleeping thread to resume run()?

 
Paul vd Maas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
My first post here, yay!
I'm writing a news ticker for Stunning-Stuff.com for other peeps to put on their web site. Everything is working until now .
My problem is that I'm using threads for the first time and don't know how to resume a sleeping thread. I want to interrupt it's sleep when I mouse over the applet.
After the string's been typed, the thread sleeps for 2 sec's. If I move my mouse over the applet during these 2 sec's it should continue the run().
I'm learning Java by looking through other people's code and reading tutorials here and there, so I would appreciate it if you'd tell me if I'm using threads in the right way . Sun's documentation regarding threads has confused me a bit .
This is the code (I know it's a lot, so I clearly marked the important bits (at the bottom)):

Thanks a LOT! I really appreciate any help .
Paul
(Edit by DW: Code block reformatted a bit to decrease width)
[ March 06, 2004: Message edited by: David Weitzman ]
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like

Should be

The event handling method is run in a Swing thread for event handling, so calling Thread.currentThread() going to return the current thread running that called that method -- in this case a Swing created thread.
 
Paul vd Maas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm. Thanks for your help, David. But I may have had the wrong idea about interrupt().
What Runner.interrupt() does is stop the applet completely. That's not what I want. I want to stop the thread from sleeping so it can execute the rest of the code and go on with the loops.
I'll try to explain what the applet should look like for the user:
First a string is typed out. If the user hovers over the applet, the applet finishes typing the line, but does not scroll to the next line until the user's mouse exits the applet. After this the line is showed for 2 seconds. If the user hovers over the applet during these two seconds, I want this sleep process to stop and the code in the run() should continue executing. This will result in the applet doing nothing untill the user takes his mouse off. If he does this it will restart the 2 sec sleep and after that the applet will scroll to the next line. Unless, of course, the user hovers his mouse over the applet again.
I have everything working, except the last part. When the 2 sec sleep is executed (after the line was completely typed), the user could hover his mouse over the applet and quickly take it off again, before the 2 sec's sleep ends. If he does this, the line could scroll away right after he took his mouse off.
What I want is, that the scroll will always happen 2 sec's after the mouse has exited the applet, or if the mouse never entered 2 sec's after the line was completely typed out.
I know I am a prefectionist, but I want my applet to be consistent and as I'm still learning Java I want to figure this out .
I hope my explanation was clear . It's a bit hard to explain though. It would probably help if you compile the code and try it out.
Thanks a bunch. I really, really, really appreciate this .
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stopping is due to this code:

Removing the return statement will allow the loop to continue execution after an interrupt.
 
Paul vd Maas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aah, of course . That did it!
Now let's see if I can make all of my other wishes come true .
Thanks a lot!!!
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could always use wait() instead of sleep(). That way if you wanted to wake the thing you can simply call notify() instead of rudely interupting such a plesant sleep.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!