Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

simple java question

Posted on 2014-01-05
3
Medium Priority
?
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 28

Accepted Solution

by:
dpearson earned 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

610 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