[android-developers] Re: Thread lifetime

2012-05-11 Thread RLScott
OK, maybe that question was too big.  All I really need to know is can
I create a static thread that runs throughout the lifetime of the app
and not worry about thread termination, trusting that if the OS needs
to stop my app's process then it will close down that thread neatly
without system resources left hanging?  The thread does not deal with
files.  It's only interaction with the rest of the app is through
static variables.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Thread lifetime

2012-05-11 Thread RLScott
OK, I got it working, and here is how I did it. I defined an extension
of Thread:

public class AudioInputThread extends Thread{
  private boolean requestWorking = false;
  private boolean isWorking = false;

public void run()
{
  while(true)
  {
try
{
  if(isWorking)
  {
 //..read and analyze a block of audio..
 //..when we want to post to the main activity, we do this:
  synchronized(Main.audioThreadSync){
  if(Main.activeInstance != null)
 
Main.handler.post(Main.activeInstance.doUpdatePhaseDisplay);
}
if(!requestWorking)
  isWorking = false;
  }
  else  //..isWorking is currently false
  {
if(requestWorking)
  isWorking = true;
else
  Thread.sleep(2000);
  }
}  //..end of try block
catch (InterruptedException ex) { /* just loop back and do more
work */ }
  }  //..end of while(true) block
}  //..end of the run() method for this thread

public void startWorking()
{
  if(! isWorking )
  {
requestWorking = true;
this.interrupt();
while( ! isWorking )//..wait for the request to be
acknowledged
  Thread.yield();
  }
}

public void stopWorking()
{
  if(isWorking)
  {
requestWorking = false;
this.interrupt();
while( isWorking )
  Thread.yield();
  }
}

}  //..end of this class definition

So the thread is always running, but it does nothing if isWorking is
false.  The interrupt is to force an early termination from the
sleep(2000). The object audioThreadSync is just a static
syncrhonization object in Main (the main activity) to protect accesses
to activeInstance.  The main activity sets activeInstance to "this" in
onResume() and sets it to null in onPause().  If activeInstance is
null then this audio thread can continue to read and analyze data
using all static variables.  It just can't post any results to the
main activity because the main activity might not exist. The use of
requestWorking and isWorking ensures that the thread will only change
isWorking at safe times.  Obviously the main activity must call
stopWorking() before shutting down the AudioRecord object.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Thread lifetime

2012-05-13 Thread RLScott
Thanks.  So now that I know about Object.wait() and Object.notify()
then I will use that instead.  There is nothing special about the 2-
second wait.  I just didn't know how to do an infinite interruptable
sleep(), which is what I wanted to do all along.

On May 13, 2:00 pm, Dianne Hackborn  wrote:
> As a general rule, if you are using sleep(), you are doing something wrong.
>  If you want your thread to stop and wait for more work to be available,
> just use Object.wait() and when you want it to wake up call Object.notify().
>
>
>
>
>
> On Fri, May 11, 2012 at 2:46 PM, RLScott  wrote:
> > OK, I got it working, and here is how I did it. I defined an extension
> > of Thread:
>
> > public class AudioInputThread extends Thread{
> >  private boolean requestWorking = false;
> >  private boolean isWorking = false;
>
> > public void run()
> > {
> >  while(true)
> >  {
> >    try
> >    {
> >      if(isWorking)
> >      {
> >         //..read and analyze a block of audio..
> >         //..when we want to post to the main activity, we do this:
> >          synchronized(Main.audioThreadSync){
> >              if(Main.activeInstance != null)
>
> > Main.handler.post(Main.activeInstance.doUpdatePhaseDisplay);
> >            }
> >        if(!requestWorking)
> >          isWorking = false;
> >      }
> >      else  //..isWorking is currently false
> >      {
> >        if(requestWorking)
> >          isWorking = true;
> >        else
> >          Thread.sleep(2000);
> >      }
> >    }  //..end of try block
> >    catch (InterruptedException ex) { /* just loop back and do more
> > work */ }
> >  }  //..end of while(true) block
> > }  //..end of the run() method for this thread
>
> > public void startWorking()
> > {
> >  if(! isWorking )
> >  {
> >    requestWorking = true;
> >    this.interrupt();
> >    while( ! isWorking )    //..wait for the request to be
> > acknowledged
> >      Thread.yield();
> >  }
> > }
>
> > public void stopWorking()
> > {
> >  if(isWorking)
> >  {
> >    requestWorking = false;
> >    this.interrupt();
> >    while( isWorking )
> >      Thread.yield();
> >  }
> > }
>
> > }  //..end of this class definition
>
> > So the thread is always running, but it does nothing if isWorking is
> > false.  The interrupt is to force an early termination from the
> > sleep(2000). The object audioThreadSync is just a static
> > syncrhonization object in Main (the main activity) to protect accesses
> > to activeInstance.  The main activity sets activeInstance to "this" in
> > onResume() and sets it to null in onPause().  If activeInstance is
> > null then this audio thread can continue to read and analyze data
> > using all static variables.  It just can't post any results to the
> > main activity because the main activity might not exist. The use of
> > requestWorking and isWorking ensures that the thread will only change
> > isWorking at safe times.  Obviously the main activity must call
> > stopWorking() before shutting down the AudioRecord object.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  All such
> questions should be posted on public forums, where I and others can see and
> answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Thread lifetime

2012-05-15 Thread Hilario Perez Corona
You may try to combine onPause with onWindowFocusChanged to pause the audio 
thread. I think you could try it out and see if it fixes your problem...

Pause te music only if your app was paused and focus was lost...

On Thursday, May 10, 2012 1:04:38 PM UTC-7, RLScott wrote:
>
> I have a working audio input app that uses the AudioRecord class for 
> low-level PCM continuous audio processing.  The audio stream is read 
> and analyzed by a separate thread which repeatedly calls 
> audioRecord.read to process the microphone audio one block at a time. 
> Currently I create and start up this thread in my main activity's 
> onResume() and tear it all down in onPause().  However this has led to 
> a nuisance if the device is rotated during certain critical periods 
> where I really need to analyze uninterrupted audio.  What I would like 
> to do is restructure my app so that the audio thread continues to live 
> and operate through an onPause() and onResume() cycle.  That means my 
> linkage to my main activity is going to be quite a bit different. 
> Currently I have instance variables to hold a pointer to the thread 
> object and to host the runables that are posted when certain points in 
> the analysis thread are reached.  What is the best practice for 
> accomplishing this with a thread that can live beyond the activity's 
> onPause()? 
>
> Should I just use static variables to hold the pointer to the thread? 
> What about app shutdown?  If I have a thread that continues to live 
> after an onPause(), which is the latest method I can count on being 
> called in the event of an app shutdown, then how does that thread ever 
> get killed when the OS wants to kill my app?  Will that statically- 
> created thread cause the system to hang?  Whenever I use threads (in 
> Windows and iPhone, for instance) I have always taken responsibility 
> for shutting down any thread that I create.  But in Android, the 
> calling of onDestroy() is not guaranteed in the activity lifetime. 
> Only onPause() is guaranteed. 
>
> My app has been working great with the audio thread lifetime being 
> tied to the lifetime of my main activity, but now that I am 
> considering severing that tie, I am not so sure what will happen to 
> that thread.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Re: [android-developers] Re: Thread lifetime

2012-05-11 Thread Mark Murphy
On Fri, May 11, 2012 at 7:23 AM, RLScott  wrote:
> OK, maybe that question was too big.  All I really need to know is can
> I create a static thread that runs throughout the lifetime of the app
> and not worry about thread termination, trusting that if the OS needs
> to stop my app's process then it will close down that thread neatly
> without system resources left hanging?  The thread does not deal with
> files.  It's only interaction with the rest of the app is through
> static variables.

Use a service to manage your thread, and ensure that the user has the
ability to stop the service when the user no longer wants it running.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Training in DC: http://marakana.com/training/android/

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: Thread lifetime

2012-05-11 Thread Kostya Vasilyev
If you just create your thread, it will keep running for as long as the 
process is alive.


You won't want a thread doing something CPU intensive continuously, so 
presumably some kind of queue with "work items" would be a good way to 
"put it on pause".


As far as termination, threads are contained in processes, so when your 
app's process is killed, all its threads, including any that you create, 
will just disappear (e.g. binder threads, WebViewCoreThread, the WebView 
cookie saving thread, AsyncTask threads, etc, etc, etc).


You might also want to consider one of: HanderThread, AsyncTask, 
IntentService, or a blocking queue...


-- K

11.05.2012 15:23, RLScott написал:

OK, maybe that question was too big.  All I really need to know is can
I create a static thread that runs throughout the lifetime of the app
and not worry about thread termination, trusting that if the OS needs
to stop my app's process then it will close down that thread neatly
without system resources left hanging?  The thread does not deal with
files.  It's only interaction with the rest of the app is through
static variables.



--
Kostya Vasilyev

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: Thread lifetime

2012-05-13 Thread Dianne Hackborn
As a general rule, if you are using sleep(), you are doing something wrong.
 If you want your thread to stop and wait for more work to be available,
just use Object.wait() and when you want it to wake up call Object.notify().

On Fri, May 11, 2012 at 2:46 PM, RLScott  wrote:

> OK, I got it working, and here is how I did it. I defined an extension
> of Thread:
>
> public class AudioInputThread extends Thread{
>  private boolean requestWorking = false;
>  private boolean isWorking = false;
>
> public void run()
> {
>  while(true)
>  {
>try
>{
>  if(isWorking)
>  {
> //..read and analyze a block of audio..
> //..when we want to post to the main activity, we do this:
>  synchronized(Main.audioThreadSync){
>  if(Main.activeInstance != null)
>
> Main.handler.post(Main.activeInstance.doUpdatePhaseDisplay);
>}
>if(!requestWorking)
>  isWorking = false;
>  }
>  else  //..isWorking is currently false
>  {
>if(requestWorking)
>  isWorking = true;
>else
>  Thread.sleep(2000);
>  }
>}  //..end of try block
>catch (InterruptedException ex) { /* just loop back and do more
> work */ }
>  }  //..end of while(true) block
> }  //..end of the run() method for this thread
>
> public void startWorking()
> {
>  if(! isWorking )
>  {
>requestWorking = true;
>this.interrupt();
>while( ! isWorking )//..wait for the request to be
> acknowledged
>  Thread.yield();
>  }
> }
>
> public void stopWorking()
> {
>  if(isWorking)
>  {
>requestWorking = false;
>this.interrupt();
>while( isWorking )
>  Thread.yield();
>  }
> }
>
> }  //..end of this class definition
>
> So the thread is always running, but it does nothing if isWorking is
> false.  The interrupt is to force an early termination from the
> sleep(2000). The object audioThreadSync is just a static
> syncrhonization object in Main (the main activity) to protect accesses
> to activeInstance.  The main activity sets activeInstance to "this" in
> onResume() and sets it to null in onPause().  If activeInstance is
> null then this audio thread can continue to read and analyze data
> using all static variables.  It just can't post any results to the
> main activity because the main activity might not exist. The use of
> requestWorking and isWorking ensures that the thread will only change
> isWorking at safe times.  Obviously the main activity must call
> stopWorking() before shutting down the AudioRecord object.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en