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
New Developer
Posts: 53
Registered: ‎06-07-2011
My Device: 9800 torch
My Carrier: o2
Accepted Solution

Mutlithreading BlackBerry

I'm having trouble creating threads for background work, whichever way I create them they always seam to hang the UI while they are executing. Thats not the effect I'm going for. 

 

I think there are three ways to create threads 

 

class Sync extends Thread
	{
		public void run() {
			//do stuff
		}
	}

Sync sync = new Sync();
UiApplication.getUiApplication().invokeLater(sync);

 

class Sync implements Runnable
	{
		public void run() {
			//do stuff
		}
	}

Sync sync = new Sync();
UiApplication.getUiApplication().invokeLater(sync);

 

 

class Sync implements Runnable
	{
		public void run() {
			//do stuff
		}
	}

Sync sync = new Sync();
new Thread(sync).start();

 

think there are variations on these methods but this is the main method. However as I stated above the UI is alwyas hanging waiting for these threads to finish. I even put a 5min wait in and it waitied 5mins before giving control back to the user. 

 

Can someone help me, create a background thread which runs independantly of the Ui thread allowing the two to execute. 

 

Developer
Posts: 40
Registered: ‎09-08-2011
My Device: 8900
My Carrier: AIS

Re: Mutlithreading BlackBerry

[ Edited ]

Why use "invokeLater"? You'd be waiting for the event thread, only to jump off it again.

 

Anyway, BlackBerry's Java uses temporal multithreading - it's what I'd call "co-operative" - this is why it has Thread.yield()

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Mutlithreading BlackBerry

Just to adding a little to the above...

 

What you call 'hanging the UI' is technically known as blocking the Event Thread.  The problem, as is explained below, is that you are not actually starting a new Thread in two of the the three methods you have given here.  

 

Method 1:

 

class Sync extends Thread
{
public void run() {
//do stuff
}
}
Sync sync = new Sync();
UiApplication.getUiApplication().invokeLater(sync);

 

This looks like it creates a Thread, and it does actually create a Thread Object.  but it is not run as a Thread.  All this does is take the Runnable part of the Thread (i.e. the run() method) and execute it on the Event Thread.  So this will actually block the Event Thread.

 

Method 2:

class Sync implements Runnable
{
public void run() {
//do stuff
}
}

Sync sync = new Sync();
UiApplication.getUiApplication().invokeLater(sync);

 

As for Method 1, except that this does not create a Thread Object..

 

Method 3:

class Sync implements Runnable
{
public void run() {
//do stuff
}
}

Sync sync = new Sync();
new Thread(sync).start();

 

This actually does create and start a background Thread.  If you did this, it would not block the Event Thread.

 

Method 4:

 

This is what I generally do:

 

Thread backgroundThread = new Thread() {

public void run() {

// code in here

}

};

backgroundThread.start();

 

Or doing this in your 'style'

class Sync extends Thread{
public void run() {
//do stuff
}
}

Sync sync = new Sync();
sync.start();

 

 

Hope this is clear.

New Developer
Posts: 53
Registered: ‎06-07-2011
My Device: 9800 torch
My Carrier: o2

Re: Mutlithreading BlackBerry

Thanks peter was secretly hoping you'd see the post and reply. 

 

Judging by your reply the "invoke..." functions seam pretty useless?

 

tried your way and its better, little lag but much better.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Mutlithreading BlackBerry

invokelater is a very useful function....

 

You use it when you DO want to run on the Event Thread.  So for example, after you have processed in your background Thread, you want to push a new Screen, you do that in a Runnable that you invoke using invokelater. 

 

Hope this makes sense.

 

BTW, you should not see any lag at all as a result of running a Background Thread.  My experience is that having a background Thread has minimal impact on the foreground UI.  If you are still seeing lag, there is is probably some other additional processing you have not figured out yet.  So keep an eye on it, and debug the code if you can pin point a spot that it happens on.

New Developer
Posts: 53
Registered: ‎06-07-2011
My Device: 9800 torch
My Carrier: o2

Re: Mutlithreading BlackBerry

Turns out I found a use for it, means I don't I dont have to create an interface to pass back to the UI thread.

Sometimes It does hang while the background thread does its thing. Maybe background thread consuming to many resources