Solved

simple java question

Posted on 2014-01-05
3
311 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys.  Things that make you go hmmmm.....  :o)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

763 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

9 Experts available now in Live!

Get 1:1 Help Now