Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BlackBerry threading model

I've read a lot of comments mention in passing that the BlackBerry threading model deviates from the Java standard and can cause issues, but no amount of googling has enlightened me on what this means exactly.

I've been developing a fairly large business application for the BlackBerry and, although I don't really have any previous experience with Java multi-threaded applications, haven't come across any issue that we've been able to blame on threading, other than what we caused ourselves.

Can someone describe exactly how the BlackBerry threading model is different, and how I as a developer should take that into account? Obviously any links on the topic would also be great.

like image 908
roryf Avatar asked Dec 10 '08 14:12

roryf


2 Answers

1.UI User interface operations always need to run in the UI thread. To execute such functions like LabelField.setText("foo"); you need to do:

UiApplication.getUiApplication().invokeLater(new Runnable(){
  public void run(){
    myLabelField.setText("foo");
    myLabelField.setDirty(true);
  }
});

Pretty easy, huh?

2.Network Network operation should never run within the UI thread. To do such things do:

new Thread(){
  public void run(){
    HttpConnection hc = 
            (HttpConnection)Connector.open("http://www.stackoverflow.com");
  }
}.start();

These two main principle are very important. You should always take care of in which thread you are operating.

like image 166
kozen Avatar answered Oct 11 '22 07:10

kozen


Two quirks I found in the blackberry forums:

  1. Vector is not thread safe.
  2. If you are creating a native blackberry CLDC app, a static is not necessarily a singleton over the VM.
like image 26
Jacques René Mesrine Avatar answered Oct 11 '22 07:10

Jacques René Mesrine