0

I have a simple question

does it make any difference to try suspending a thread(in my situation)?

my thread is just an image rotator (my minSDK is 8 so must use matrix, not animation)

i have just asked a question about how to suspend a thread. everyone offered using:

while(isPaused){
    try {Thread.sleep(25);}catch(...){...}
}

and my code is like this :

while(true){
    //rotate image 15 deg
    try {Thread.sleep(25);}catch(...){...}
}

they offer to use the first while inside the while(true) does it make much difference ?

(I don't have to stop the thread. I can just make the rotating image invisible)

Simpler :

Does it make difference to use this to pause a thread :

while(true){
    while(isPaused){
        try {Thread.sleep(25);}catch(...){...}
    }
    //rotate image 15 deg
    try {Thread.sleep(25);}catch(...){...}
}

or there isn't any problem leaving the code to rotate an invisible image ?

3 Answers3

1

Edit: considering this is for a custom loading indicator, please don't use a thread. That is overkill for such a simple thing. Have a look at this answer on how to create custom loading indicators using animations.

https://stackoverflow.com/a/8129496/2910492

Community
  • 1
  • 1
Jeffrey Klardie
  • 3,020
  • 1
  • 18
  • 23
  • OK it's true then my problem is here : when continueRotating becomes false the run() method in thread finishes ... but i must pause the thread then start it again pause then start in fact my threat is a loading icon rotator. then i should use it many times and this isn't 25sec its 0.025 sec – Ebrahim Tahernejad Dec 24 '13 at 19:21
  • Not sure, but I think it has been supported since API level 1. Just try it, and you'll find out soon enough. **Edit**: yes, it's supported since the beginning. – Jeffrey Klardie Dec 24 '13 at 19:49
0

This has entirely to do with your application. Some more information would be useful, but in general, it seems unnatural to have an image rotating while things are supposed to be paused. Even if the image is invisible, it would (presumably) come back in a different orientation than it was when paused.

Also, the computer's resources must be consumed to continue rotating the image, which is probably not desirable. So I'd revise your code to keep things from executing while paused as such:

while(true){
  if(!isPaused){
    //rotation code
  }
  try {Thread.sleep(25);}catch(...){...}
}

Doing things this way eliminates some re-use of code. When you have a way to eliminate code and not repeat yourself, it is almost always a good idea to employ it. Repeated code means repeated errors and makes more work for you when you need to change something. If you have to copy and paste something, stop and do some soul-searching: there is most likely a better way.

user2503846
  • 924
  • 1
  • 7
  • 10
0

You should use the "condition" instead of "true" in your while loop. That condition should be useful to complete the working of the thread.

For e.g. you may not want to make the thread orphan and keep on running even if application gets ended. So in onDestroy you can make that conditional flag false and your while loop gets failed hence thread completes it's task.

Dalvinder Singh
  • 2,129
  • 4
  • 21
  • 20