1

I want to make an auto synchronize in my application that will happen every day at 9:00 pm. I am doing this in the way of :

try { Thread.sleep(24hours); } catch (Exception ex) { }

But the problem is when the user turns off his device before 24 hours, When turning it on the thread will sleep again for 24 hours.

So I am thinking of a way to manage this depending on the clock of the system. When the device has the time of 9:00pm, the application will be alerted and I will do the work.

Is it possible to make this?

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Farid Farhat
  • 2,300
  • 1
  • 16
  • 29

6 Answers6

2

Have you taken a look at Quartz (http://www.quartz-scheduler.org/)? Maybe it will be applicable.

smp7d
  • 4,947
  • 2
  • 26
  • 48
2

I recommend using one of the available, open source java schedulers already made: List of Open Source Job Schedulers in Java

The one I'm familiar with is the first in that list: Quartz

... Looks like someone beat me to it. (The answer above was added while I was writing this one)

But just to expand real quick, you can easily set up "jobs" that run in the background, separate from the user. Now I've personally used this with websites and not with devices that users can turn off and on. That being said I'm sure what you're trying to do has been tackled before and you should give quartz a try, it can probably do what you need.

Good luck, -Asaf

Asaf
  • 815
  • 1
  • 9
  • 25
  • Thanks for your comment. But dont you think that Quartz in his back end is using thread.sleep? he is surely... If yes then this will not help me I guess. I don't know I will try this and see what happens – Farid Farhat Sep 13 '11 at 13:46
  • Why don't you just utilize an if statement that asks if the quartz job is already in existence and running, and if so, don't restart the job? – Asaf Sep 13 '11 at 14:28
  • This seems like overkill for a mobile device. Also, is there a java-me version of quartz? – Michael Donohue Sep 13 '11 at 21:58
  • Well, I don't have a lot of experience with mobile devices, so I'll take your word on it. And for that question, I don't know... – Asaf Sep 13 '11 at 22:23
2

You want to schedule your app to run at a particular time. BlackBerry's OS supports this. See Schedule a BlackBerry application

Community
  • 1
  • 1
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
0

The problem might be resolved by running simple background thread.

  public static void main(String[] args) {
        Thread myJobThread = new Thread(this, "My background job");
        myJobThread.setDaemon(true);
        myJobThread.start();
    }

    // check the time every minute
    private int checkRate = 60000;

        @Override
        public void run() {
            while(true) {
                Date now = new Date();
                if (now > 9a.m.) {
                    // do your stuff
                }
                try {
                    Thread.sleep(checkRate);
                } catch (InterruptedException e){
                    // handle ex
                }
            }
        }
Michal Vician
  • 2,486
  • 2
  • 28
  • 47
  • 1
    I dont want to use the background thread. I am searching for a better way to do that – Farid Farhat Sep 13 '11 at 13:48
  • did'nt you forget a loop in your thread? or is that `setDaemon(true);` responsible for that? – Neifen Sep 13 '11 at 13:50
  • Yes, you can use quartz and similar things. The question is if you need it. In fact quartz also uses background threads. If you wan't your application to be small and you don't need any complex job management then background thread is the way I think. – Michal Vician Sep 13 '11 at 13:53
  • I use background threads in my application and threads so no need for me to use Quartz. I am doing what he will do certainly – Farid Farhat Sep 13 '11 at 13:59
0

You could consider using the RealTimeClockListener. You will then write a method clockUpdated() that will be called by the OS every minute. In there you can do your calculation to determine if it is time to execute your code.

Scott W
  • 9,742
  • 2
  • 38
  • 53
-1

Maybe you can do it with Calendar:

    boolean nine=false;

    //until it's nine'o clock
    while(!nine){

      Calendar calendar=Calendar.getInstance();
        if(calendar.get(Calendar.HOUR_OF_DAY)==9&&calendar.get(Calendar.MINUTE)==0&&calendar.get(Calendar.SECOND)==0&&calendar.get(Calendar.MILLISECOND)==0){
            nine=true;
                nine=true;
            }
            else {
                sleep(yourTime);
            }
    }

    //...whatever should happen now...

   //put the boolean back to false;
    nine=false;
Neifen
  • 2,546
  • 3
  • 19
  • 31
  • Yeah that is done. But in your code you will make a while true clause which is not the right way to do that. – Farid Farhat Sep 13 '11 at 13:48
  • i make a while false clause... but why is that not the right way? – Neifen Sep 13 '11 at 13:51
  • I dont want to make a while true clause. I am thinking of another way that will not use the Thread.sleep and the while true clause. I did this with the algorithm you wrote up there before but trying to make things better – Farid Farhat Sep 13 '11 at 13:53
  • 1
    That's not a good solution at all, even if it did work. It would busy-loop, eating CPU until the time is reached. But it doesn't work. Those constants in the Calendar class are just that: **constants**. They do not reflect the current time at all. – Mat Sep 13 '11 at 17:15
  • Also you can simplify this code just in 3 lines like: Calendar calendar = Calendar.getInstance(); while(calendar .get(Calendar.HOUR_OF_DAY)!=9 && calendar.get(Calendar.MINUTE)!=0){sleep(yourTime);} – kornero Feb 15 '12 at 08:11