Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I schedule a particular thread in Blackberry

I want to auto-schedule a thread with a particular time interval. I also need to execute this in the background continously without hangs to the device.

I have tried this with Application Manager Class but it's for application scheduling and I need to schedule thread within the application.

like image 878
Rishabh Avatar asked Oct 29 '09 12:10

Rishabh


People also ask

Can threads be scheduled?

Threads are scheduled for execution based on their priority. Even though threads are executing within the runtime, all threads are assigned processor time slices by the operating system. The details of the scheduling algorithm used to determine the order in which threads are executed varies with each operating system.

What are the two types of thread scheduling?

Scheduling of threads involves two boundary scheduling, Scheduling of user level threads (ULT) to kernel level threads (KLT) via lightweight process (LWP) by the application developer.

What is the algorithm used in thread scheduling?

There are two algorithms, used for Java thread scheduling. Time sliced based scheduling algorithm, also known as Round-Robine algorithm. Pre-emptive scheduling.

What is Java thread scheduler?

A component of Java that decides which thread to run or execute and which thread to wait is called a thread scheduler in Java. In Java, a thread is only chosen by a thread scheduler if it is in the runnable state.


3 Answers

I would use TimerTask:

public class MyScreen extends MainScreen {
    private Timer mTimer;
    public MyScreen() {        
        mTimer = new Timer();
        //start after 1 second, repeat every 5 second
        mTimer.schedule(mTimerTask, 0, 5000);
    }

    TimerTask mTimerTask = new TimerTask() {
        public void run() {
            // some processing here
        }
    };
}

see BlackBerry API Hidden Gems (Part Two)

like image 112
Maksym Gontar Avatar answered Nov 16 '22 22:11

Maksym Gontar


use UiApplication.getUiApplication().invokeLater() it accepts a delay and repeat parameters and will perform exactly what you need.

EDIT

I know this post is old but this is by far the best option to schedule repeating events and I would like to add that to stop the scheduled event the following is required:

//Start repeating "runnable" thread every 10 seconds and save the event ID
int eventId = UiApplication.getUiApplication().invokeLater(runnable, 10000, true);

//Cancel the repetition by the saved ID
UiApplication.getUiApplication().cancelInvokeLater(eventId);
like image 25
reflog Avatar answered Nov 16 '22 20:11

reflog


Assuming you want it to run a thread on device startup: Create a second project and list it as an alternate entry point. In your UiApplication or Application main(), check the argument passed to the project. Do your periodic stuff there via Thread.sleep and don't call enterEventDispatcher.

search for "autostart": http://docs.blackberry.com/en/developers/deliverables/1076/development.pdf

Or if you want to do something once a user "starts" it, then look into creating a new thread to do your timing stuff. Override your screen's onClose() and use Application.getActivation().deactivate() to throw the screen into the background.

Or there's a other ways to do something like this like invokeLater, etc. Maybe eventlisteners may do what you need, but you didn't give a lot of details.

like image 1
nope Avatar answered Nov 16 '22 22:11

nope