Solved

simple java question

Posted on 2014-01-05
3
314 Views
Last Modified: 2014-01-09
An example in "android developer's cookbook", p. 57, shows how to stop a thread with the following:
If(myThread != null) {
   Thread dummy = myThread;
   MyThread = null;
   dummy.interrupt();
}

My question is, why not just do this?
If(myThread != null) {
   myThread.interrupt();
   myThread = null;
}

Are these not equivalent?

Thanks,
Mike
0
Comment
Question by:thready
3 Comments
 
LVL 27

Accepted Solution

by:
dpearson earned 250 total points
ID: 39757752
Clearly both pieces of code are virtually identical.

The only difference is that the first one appears to be trying to play some game with the garbage collector.  The way it's written suggests some internal knowledge of the collection algorithm that might allow it to collect the resources associated with the thread if there were no references to the thread object, except for local stack variables, at the time of the interrupt being called.

I don't know enough about how Android's collector works to know if that would actually work.

If it does then certainly that behavior should have been very carefully commented in the cookbook to explain why they were doing this dance.

Even if that dance does work on Android, I think the code you wrote below is still the correct choice almost always.  The other path is at best a tiny optimization and definitely confusing to see.  It should only be used if (a) it really works and (b) recovering those extra resources is critical to the overall app.

Doug
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 250 total points
ID: 39757759
The first style would not let the object referenced by the myThread variable be collected by the garbage collector, since the dummy variable still references it. It might be trying to prevent other threads from also calling interrupt(), but, without a synchronized block, that code does not actually guarantee anything.
0
 
LVL 1

Author Closing Comment

by:thready
ID: 39769872
Thanks guys.  Things that make you go hmmmm.....  :o)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question