0

Is there a way in stopping thread.sleep on button click? like for example? i clicked the 1st button for sleep and then the second button is for stopping the thread while on sleep? i have tried thread.stop() and thread.interrupt and it's not working.

ALD
  • 23
  • 2
  • 11

2 Answers2

0

Thread.interrupt() will cause a sleeping thread to unblock ... assuming you interrupt the right thread.

Thread.stop() is deprecated, and SHOULD NOT BE USED.

However ...

Is there a way in stopping thread.sleep on button click?

This does not make a lot of sense to me. Unless you have done something really strange (and wrong), a thread does not "sleep on a button click". And you certainly should NEVER write an input event listener (for example an "on click" listener) that calls sleep(...). Since input event listeners get called by the event handler thread, if you sleep you are going to lock up the UI.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • thanks for your answer. yes I used the thread.sleep on an on click listener. i have to do that so that the sounds that are called in mediaplayer won't overlap. is there any other way or alternative way for thread.sleep? – ALD Mar 07 '15 at 07:24
  • This page gives an overview of the MediaPlayer API, and shows that there API methods on the MediaPlayer class itself for pausing and resuming the player. You should be using that rather than Thread.sleep. – Stephen C Mar 07 '15 at 09:45
0

Call the interrupt() method on your thread. This will cause the sleep to be cancelled and an InterruptedException will be thrown.

Fahim
  • 12,198
  • 5
  • 39
  • 57