Solved

simple java question

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DGTEC 10.1" Quad Core Android Tablet 18 95
micro services spring boot application error 3 148
simple java question 3 44
Protecting a SKY 4.0 (Android) devise 15 102
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now