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: 13
Registered: ‎02-15-2009
My Device: Not Specified
Accepted Solution

Callback from child thread to parent thread

Hello,

 

I have a question regarding inter-thread communications.

 

I have a class Class A (parent) for example which creates a child class B running in its own thread.

I want to have the child thread callback to its parent.

 

If I make a cal from my class B run such a parent.somethingHasHappened() are there any issues I should be aware of with respect to blocking.

 

For example if I make a call on a function in the parent class from the child could this potentially be blocked due to the parent thread being busy (alternatively what does happen if I call parent.somethingHasHappened() and the parent thread is busy, or is there any thing that would prevent me from making such a call)

 

Apologies if this is a dumb question I;m getting my head around the Java threading model after years of Symbian. 

 

 

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: Callback from child thread to parent thread

Parent thread has a flag, let say: private boolean somethingHasHappened;

 

and parent class has a public method:

 

public synchronized void setHappenedFlag(boolean happened) {

 

this. somethingHasHappened = happened;

 

}

 

 

child thread calls this method to set this flag to true (or false).

 

Parent thread checks this flag from time to time and acts accordingly.

New Developer
Posts: 13
Registered: ‎02-15-2009
My Device: Not Specified

Re: Callback from child thread to parent thread

Thanks that makes sense. 

 

If I create my 'child' thread from within my application's main thread what is the typical mechanism for polling any flags set from my child thread? 

 

What I'm aiming for is having status information from the child working thread available to the UI without any blocking of the UI. 

Developer
Posts: 5,339
Registered: ‎09-20-2008
My Device: ***
My Carrier: ***

Re: Callback from child thread to parent thread

Pooling mechanism may depend on the task.

 

For example - implement a thread to check the flag value and run it on regular basis (use TimerTask class).

 

Or implement a while() loop and check this flag on every iteration.

Highlighted
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Callback from child thread to parent thread

Generally you are better off searching sun.com for this ( google confined to site:sun.com ).

 

http://www.google.com/search?source=ig&hl=en&rlz=1G1GGLQ_ENUS312&q=site%3Asun.com+volatile+thread+co...

 

 

In the simple case of just wanting to expose a flag, volatile would do but you generally need synchronized

for read-modify-write cycles. When they deprecated

things like Thread.stop(), seems to have also gotten away from using synchronized methods like they

used to always do. ( Earlier, this made it difficult to write applets that didn't at least occasionally hang browsers

and there were all kinds of wierd synchronizations running around).

 

These guys are trying to scare people into synchonrized versus volatile and talking about recent changes, but you get some idea of issues and evolution, see page 26 for example,

 

http://developers.sun.com/learning/javaoneonline/2006/coreplatform/TS-1630.pdf

 

 

For some historical perspective on this issue, see the blinker example here, where they just use null/non-null as a flag and then

go on to illustrate with more synchronized method examples that were still quite popular back then.

http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

 

private volatile Thread blinker;

    public void stop() {
        blinker = null;
    }