Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 49
Registered: ‎05-14-2009
My Device: Not Specified

Battery discharges too fast because of my app.

Hi!

In my app. I use two thread.

The first one:

 

Checker = new Thread(new Runnable() {
       public void run() {
           MainThread mainthread;
           mainthread = new MainThread();
           mainthread.start();
           while(true) {
                try {
                     mainthread.join();
                }catch(Exception e) {}
                mainthread = new MainThread();
                mainthread.start();
           }
      }
});
Checker.start();

 

class MainThread extends Thread {

      public void run()   {

             while(true) {

                       *

                       *

                       *

                       Thread.sleep(1000 * 60 * 30);//Delay 30 min

             }

      }

}

 

So, without my app. the device works for 4 days, with my app. for 12 hours.

 

Why so?

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Battery discharges too fast because of my app.

What exactly is your code doing every 30 minutes? The code you've posted so far does not look like a serious drain on the battery.

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Battery discharges too fast because of my app.

Any idea how sleep is implemented? You'd think in mobile JVM it would be ok but who know, try also wait()

as this may be better, hard to know. Absent that, you want to look for anything obvious- tight loops, radio, accelerometer

channel open, LED stuck on ,etc. Power drain mes dissipation- does device get hot at all? If so, at least

you have a diagnostic, RIM has a better temp API that may be of some help, but AFAIK no current measure which

woul dbe more direct.

 

( I think this stupid forum ois too complicated for firexoc]

,c as keys lag for up  to a minute after typing, wtf??? anhone else having problems. I've noticed many site have "upgraded" html that uses all kinds of wizbang features that screw up everything.... )

New Developer
Posts: 49
Registered: ‎05-14-2009
My Device: Not Specified

Re: Battery discharges too fast because of my app.

My code checks mail messages, does a data processing and saves the result to a file and backs to the sleep. That's all. There's one thing I'm thinking about - is it possible because of I don't close the file with results of a data processing?

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Battery discharges too fast because of my app.

I guess the code that is run every 30 minutes is quite battery intensive. Give it a try: leave the threads and the 30-minute sleep there, but don't actually run your message processing code. I bet the battery drain will return back to normal.

Developer
Posts: 499
Registered: ‎06-24-2008
My Device: Not Specified

Re: Battery discharges too fast because of my app.

If you want to run every 30 minutes why not just use the scheduleApplication method of ApplicationManager and have the app reschedule itself after every execution.

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Battery discharges too fast because of my app.

let us know if that works, sleep or wait(timeout) shouldn't be charging/discharging any additional capacitors

if implemented reasonably.

New Developer
Posts: 49
Registered: ‎05-14-2009
My Device: Not Specified

Re: Battery discharges too fast because of my app.

 

Hi

 

The problem has been solved!

Functions Thread.sleep() and Thread.wait() don't solve the problem.

The good style:

 

class MainThread extends {

    for (;Smiley Wink {

         try {

             DataTransfer datatr = new DataTransfer();

             datatr.start();

             datatr.join();

             Thread.sleep(/*Delay*/);

         }catch(Exception e){}

    }

}

class DataTransfer extends Thread {

    public void run() {

         //Transfer work;

    }

}

So, I'm using a separate Thread for a data transfer and after a transmission it exits. Till now my test has been working for 2 days with delay 5 min (old value 30 min).

 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Battery discharges too fast because of my app.

The only conceptual difference I see is that now one of your thread is blocked on join for less than 30 minutes (in the past, it would wait on the other thread sleeping for 30 minutes, now the other thread does not sleep for 30 minutes). I highly doubt that a long Thread.join() would waste battery. Are you really sure that this was change that fixed your battery consumption issue?

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Battery discharges too fast because of my app.

it may have been encountering tight loops if doing data transfer, hard to know. You could try the

join idea and see if that is culprit per se. I've had problems with this, I think I finally fixed my problem

by eliminating some polling loops after a long time out, these appear to really add up even with

sleeps for few seconds/iteration and checking a few flags. I've still got threads to service various ques

that continue to poll even when que is empty but just need to turn on logic to die on empty que and restart

on new entry etc. Of course, polling remote server can use

radio etc. And, re wait, I meant something like dummyobject.wait(timeout) where you could use any object

but somethread.wait(timeout) should be ok but it may be notified by others, who knows. I wouldn't

suggest adding sync/locks just for the heck of it but if sleep really is that bad it was something to

consider.