Reputation: 3264
I am implementing one app related to alaram manager and brodcastreceivers.
I am repeting alarm in every minute,through the alarm manager I call brodcastresever class. In side this class I am implementing one thread. The code is as follows:
runnable = new Runnable()
{
public void run()
{
while(i>o)
{
}
}
if i>o
the thread will continue.
Another time my alaram manager call brodcast resever class. That time also one new thread is created with old thread. This increases duplication of threads.
How to avoid that?
Upvotes: 0
Views: 63
Reputation: 29199
Why not you use a queue of requests to process instead of creating new thread every time. Like:
if(queue==null)
{
queue= new ArrayList<Request>();
queue.add(request);
start();
}
public void run()
{
while(queue!=null && queue.size()>0)
{
Request request= fetchRequest();
processRequest(request);
}
}
private Request fetchRequest()
{
Request request=queue.get(0);
queue.remove(0);
if(queue.size()==0)
queue=null;
return request;
}
Note its just a prototype, not implementation.
Upvotes: 1