Jun 22 | 7:25 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Steve S. | has entered the room |
Mark M. |
hello, Steve!
|
Mark M. |
how can I help you today?
|
Steve S. |
Hi, Mark!
|
Steve S. |
Let me paste in my question:
|
Steve S. |
View paste
(6 more lines)
|
Mark M. |
well, the constructor will have completed before run() gets called
|
Steve S. |
ok
|
Mark M. |
so, that particular bit of contention can't happen
|
Steve S. |
Right. But isn't there a separate issue about visibility?
|
Mark M. |
um, well, it's private
|
Mark M. |
nothing other than ReadThread can get to mmSocket
|
Mark M. |
now, if you have other methods on ReadThread, and those methods might be called on some thread other than then the "real" thread that run() is called on, *then* you could have an issue
|
Mark M. |
though I doubt volatile is sufficient in that case anyway
|
Mark M. |
if you want to have the field be volatile, that's fine -- I don't think it is causing any problems here
|
Mark M. |
but I'm not sure that it's giving you any benefits, either
|
Steve S. |
Right. But I thought that if a variable is accessed from two different threads , changes made in one thread won't necessarily be visible from another thread.
|
Jun 22 | 7:30 PM |
Mark M. |
only if that code is happening at the same time
|
Mark M. |
and even then, the story gets complicated
|
Mark M. |
in this case, the constructor completes before run() executes
|
Mark M. |
run() will see whatever the constructor assigned to mmSocket
|
Steve S. |
That's the part I'm not sure about.
|
Mark M. |
:: shrug ::
|
Mark M. |
I have never had a problem with it
|
Mark M. |
but, again, if volatile makes you feel better, keep it
|
Steve S. |
For instance, in Effective Java in the chapter on Concurrency, it says that "Without synchronization, one thread's changes might not be visible to other threads." My understanding is that the variable could be cached, so it would be visible to the thread in which it's set, but not other threads.
|
Mark M. |
again, that is for *parallel work*
|
Mark M. |
again, the constructor will complete before run() is called
|
Steve S. |
ok. So as long as what's done in one thread completes before what's done in another thread, visibility isn't a concern?
|
Mark M. |
correct
|
Steve S. |
ok
|
Jun 22 | 7:35 PM |
Steve S. |
Another question: I'm putting the Bluetooth code in a service. I have some running threads that I shut down in onDestroy(). I know that onDestroy() won't always be called, for instance, if there's a low memory situation. But in that situation, would those threads be shut down?
|
Mark M. |
in that situation, the process is terminated
|
Mark M. |
in which case, the threads, the Bluetooth connection, and all your objects, go *poof*
|
Steve S. |
Ok. So if onDestroy() is called, I need to do something to shut down the threads. But if onDestroy() isn't called, I don't need to do anything?
|
Mark M. |
there are three possible scenarios:
|
Mark M. |
1. onDestroy() is called
|
Mark M. |
2. the process is terminated, in which case, there is nothing that you can do anyway
|
Mark M. |
3. you crashed with an unhandled exception, in which case you are dealing with matters in some UncaughtExceptionHandler (e.g., using ACRA for crash logging)
|
Mark M. |
#3 can be a slightly sticky wicket
|
Mark M. |
but, of course, your code will never crash, right? :-)
|
Steve S. |
of course not!
|
Steve S. |
ok. I think I understand.
|
Steve S. |
Very helpful answer. Thank you so much! No more questions today.
|
Jun 22 | 7:40 PM |
Mark M. |
happy to be useful!
|
Steve S. |
Have a great rest of the day, Mark!
|
Mark M. |
you too!
|
Steve S. | has left the room |
Jun 22 | 8:25 PM |
Mark M. | turned off guest access |