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
Trusted Contributor
lakshmanan
Posts: 144
Registered: ‎05-04-2011
My Device: blackberry mobiles
My Carrier: app development

Multi thread in timer task

Hello Friends,

 

Can anyone tell me how to run 5 threads one by one in a timertask... I am using ksoap web service...I used 5 seperate tables in database... From each thread i call a web service and store its value in its related table... I want to call all the webservices at the same time... But it takes more time... So i want to call it one by one in a particular time.. Can anyone help me..

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Multi thread in timer task

Hi @

 

 

Here are a couple of options:

 

1. Use sevaral TimerTask objects instead of threads. Each timer task object will schedule the next timer task.

 

2. Use the join method of the Thread. This will cause the current thread to wait for the thread who called the join.

 

E.

 

 

Please use plain text.
Trusted Contributor
lakshmanan
Posts: 144
Registered: ‎05-04-2011
My Device: blackberry mobiles
My Carrier: app development

Re: Multi thread in timer task

Thanks for your reply,

 

I dont know both methods.. I searched forums and didnt find the correct one.. Can you explain more by provide sample code for both methods...

 

Regards,

lakshmanan

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Multi thread in timer task

For the first option:

 

Timer timer = new Timer();

TimerTask t1=new TimerTask() {
    public void run() {
        //Do what the first thread needs to do here.
        timer.schedule(t2, 0);
    }
};

TimerTask t2=new TimerTask() {
    public void run() {
        //Do what the second thread needs to do here.
        timer.schedule(t3, 0);
    }
};

TimerTask t3=new TimerTask() {
    public void run() {
        //Do what the third thread needs to do here.
    }
};

timer.schedule(t1, 0);

 

For the second option:

 

//tArray is the thread array.
for(int i = 0; i < tArray.length; i++)
{
  tArray[i].start();
  try
  {
    tArray[i].join();
  } catch(Exception e) {}
}

 

Do note that my code was not tested or compiled but is meant to explain the general idea.

 

E.

 

Please use plain text.
Developer
silver20
Posts: 455
Registered: ‎03-04-2011
My Device: Paper cup

Re: Multi thread in timer task

If you want to do the work in a single timeline, why do you need multiple threads?

 

If it's because the access code is already implemented each in a separate Thread object, then in the timertask's run you can start the first thread, join the 1st, start the 2nd, join the 2nd, etc.

 

Please use plain text.
Trusted Contributor
lakshmanan
Posts: 144
Registered: ‎05-04-2011
My Device: blackberry mobiles
My Carrier: app development

Re: Multi thread in timer task

Thanks for your reply,

 

I used your first method.. It insert records repeatedly in database... I dont know how to stop timertask one by one if it inserts all the records in database...

 

Regards,

lakshmanan

Please use plain text.
Developer
maadani
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Multi thread in timer task

TimerTasks are just like threads and they stop when they are finish their work.

 

If the tasks keep on running it's probably because you schedule them to repeat execution or there is a loop that keeps calling the code that run the timer.

 

E.

Please use plain text.
Developer
silver20
Posts: 455
Registered: ‎03-04-2011
My Device: Paper cup

Re: Multi thread in timer task

Why can't you just use a single TimerTask with sequential calls to whatever you need to do, and forget the multiple threads or timers?

If you are going to use the 1st method, I think it would be better to implement as a single thread with a yield() or brief sleep() between each of your database accesses, than to daisy-chain timers, it would have the same effect and be simpler.
Please use plain text.