If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Java thread question

 

If I have a main thread, for example the UI of the app that starts a thread that includes waits or heavy processing how do I get a signal and optionally a return value when the thread has finished. I looked at Wait/Notify but that did not seem to fit the bill. I could probably use global events for this but I am interested in how it is best done in Java. Did some internet browsing but did not find it.

 

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
yosoh
Posts: 213
Registered: ‎07-18-2008
My Device: Not Specified

Re: Java thread question

Hi,

 

did you take a look at the Observer pattern. It might help you.:

 

http://en.wikipedia.org/wiki/Observer_pattern

 

regards,

yosoh

--------------------------------------------------
problemSolved() ? kudosPlease():kudosPlease();
Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Java thread question

[ Edited ]

look for a developer pattern named callback or observer.

you place a callback interface as an argument to the thread and call a method on the interface when it is finished.

another option, if more than one class wants the result, would be to register listeners (you can, of course, combine both).

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
m-romanuik
Posts: 114
Registered: ‎04-28-2010
My Device: Storm 9530
My Carrier: Telus

Re: Java thread question

This is definitely not the proper way of doing it because I'm not using the observer pattern. Im just too lazy to create listeners. This basically passes the field you want to speak to. So if I wanted to call repaint every 500 milliseconds.. In my run I would just have a while loop for as long as I want to update for while calling _sleep and _gameField.repaint();. Some people may say becareful with this because its not synchronize however I haven't come across any issues whether it dealt with networking, animations, or updates in textFields. Maybe that day with come but so far this has worked for me up until this point. Message me if you want more detail.

 

Place this at the Bottom of your Public Class

 

class gameThread extends Thread {
    // Variables and Fields
    private GameField _gameField;
    private final int _sleep = 25;

    public GameThread(Field gameField){
        // Initialize Variables and Fields
        _gameField = (GameField) gameField;
    }

    public void run(){
        // Sleep until next Incremenation
        try{this.sleep(_sleep);}
        catch(InterruptedException e){}
    }
}

Call it with this...

 

GameThread animationThread = new GameThread(gameField);
animationThread.setPriority(Thread.MAX_PRIORITY);
animationThread.start();

 

 

 

Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Java thread question

an observer pattern does not use listeners, it encapsulates the receiving class in an interface.

when you develop on your own only the usage of interfaces may sometimes sound too complicated, but once you develop in a team or even explain your code to somebody else it is very helpful to use such patterns.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
m-romanuik
Posts: 114
Registered: ‎04-28-2010
My Device: Storm 9530
My Carrier: Telus

Re: Java thread question

im not saying forget using the observer pattern. nor am i saying its too complicated. Im just saying for a simple task to be computed its easier to pass the object straight to the thread rather than creating 10 other classes and methods to communicate with each other. On the other hand I was always taught that the observer pattern was where the subject held an onto an array of observers and tells the array of "observers" to update() when a certain state has occured. This is exactly how a listener works is it not?

Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Java thread question

if you have only a single observer there is no need to wrap an array around it.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
m-romanuik
Posts: 114
Registered: ‎04-28-2010
My Device: Storm 9530
My Carrier: Telus

Re: Java thread question

Look, lets not put details in. I provided an alternate method that could work for something simple. If you dont agree with it, dont use it.

Please use plain text.
Developer
simon_hain
Posts: 15,908
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Java thread question

sure, we can settle for that.

my java professor would have thrown you out of the lecture room for the code though :manmad:

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Java thread question

[ Edited ]

Spent some hours looking for explanations and samples. Not to happy. like the student said to the professor after he got an explanation for the third time. "I am still confused, but at a much higher level"

 

I have a few basic premises (from WAY back) when I write code. Separate code into "viewable" "function" pieces. Make code readable and easy understandable. Make comments. Favor clarity over performance.

 

Now I have a few pieces that runs as threads. They really do not need to "reply" anything to my "main" thread. They do things like placing a call, Sending DTMF tones and killing calls.  They are separate classes and I first call the class setting parameters and then start the run method. Makes for simple and understandable code. (For me at least)

 

So now I have a class that I want to run as a thread and the main routine needs to get an answer. If possible I would like to keep the same structure as above. It could easily be implemeted with Global listening since the "main" routine is already a lobal listener.

 

I find "Inner classes" ugly. Looping around waits is ugly

 

I was just curious if there was simple Java method to accomplish this. Did not find it simple to understand how it works.

 

My background, very long time programmer, done multitasking programming in mainframes and windows. Not experienced in Java  makes me a bit "biased"  :-)

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.