7

I've looked at the BB API(5.0) and I can't find any way of serially executing a batch of threads. I know BB has a limit on the number of threads it will launch, so I don't want to launch 7 if the user clicks through things fast enough but I cannot find anything like a thread pool.

Is there an easy fix for this or do I have to create a data structure?

Nicholas
  • 7,403
  • 10
  • 48
  • 76
  • why do you want multiple threads? What about having the UI launch a single thread that does X, Y, Z? – seand Nov 23 '10 at 08:11
  • Because each event is different. I want to launch them so the UI is not being blocked on waiting for things to be done. – Nicholas Nov 23 '10 at 15:46

2 Answers2

10

If you just want to execute a bunch of tasks on a single thread serially and order isn't important, you could create a Timer object (which has its own thread) then add each task to it as a TimerTask. If you schedule it with a delay of 0 or 1, it will essentially run that task as soon as possible. And since a Timer only has a single thread, if you schedule multiple tasks concurrently, it will ensure that only one will run at a time.

Incidentally, I was talking to a RIM engineer at the BlackBerry Developer Conference this year and he said that as of OS 5.0 there are no longer limits to the number of threads -- so this is becoming less and less of a concern.

Marc Novakowski
  • 44,628
  • 11
  • 58
  • 63
  • 1
    Even if the OS upped the limits threads are still an expensive resource on a constrained env like a mobile device. It's always good practice to reduce the number of threads needed when possible. – seand Dec 03 '10 at 03:25
  • Hello all, my applications requirement is it has to access network in every page. What I am doing is I am creating an inner class that extends Thread for every screen that performs requires network connectivity, Is this a correct approach? Help of any sort is appreciated. A Y. – varunrao321 May 24 '12 at 12:55
1

I've tested Jeff Heaton's Thread Pool example on 4.5 and it works. (http://www.informit.com/articles/article.aspx?p=30483&seqNum=1).

eSniff
  • 5,713
  • 1
  • 27
  • 31