Solved

simple java question

Posted on 2014-01-05
3
313 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 26

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

786 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