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
Posts: 91
Registered: ‎06-21-2011
My Device: Blackberry Torch 9800
My Carrier: developer
Accepted Solution

Thread synchronization

Hi All,

I really don't know how is working Blackberry gui thread!!!

I have function which run some thread in which I perform my manager moving.After that moving is completed I want to set focus on manager but it isn't focus:

 

// first calling this function

// which later call animateProfile() - thread function

// and finally call animCompleted() (which must make focus on manager)

public void performAction() {
        if (null == profileClick) {
            if (null != listener) {
                listener.performAction();
            }
            initProfile();
            animateProfile();
            animCompleted();
        } else {
            Reset();
        }
    }

 

private synchronized void animateProfile() {
        Thread slide = new Thread() {
            public void run() {
                int count = 0;
                int height = profileClick.getPreferredHeight() + 4;
                while (count < height) {
                    profileClick.moveScreen(0, count);
                    invalidate();
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count += 8;
                }
                animCompleted = true;
            }
        };
        slide.start();
    }

 

private void animCompleted() {
        if (animCompleted) {
            profileClick.setFocus();
        }
    }


Any help?

Regards,
Arsen
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Thread synchronization

Well - you start your separate thread which is supposed to set your animCompleted to true after some time and immediately check whether that boolean is true. No wonder it is still false at that point.

 

The best way to handle it is to call animCompleted() from that thread instead of setting some boolean value. However, you'll have to grab the event lock before you can setFocus on that manager. This is easily done - see the following video for explanation:

What is the Event Thread?

 

However, it seems you have very little understanding of threads in general. Grab a good Java book and study this matter - it is absolutely necessary for your future career, Java or no Java.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 91
Registered: ‎06-21-2011
My Device: Blackberry Torch 9800
My Carrier: developer

Re: Thread synchronization

Thank arskadyz for reply,but I already test what you said about calling function in thread,but that thread is separate from gui main thread so calling it will cause exception.I also tried to put some Thread.Sleep(...) before checking but it's too didn't help.Can you suggest any sollution?

Regards,
Arsen
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Thread synchronization

[ Edited ]

... and that's why I referred you to that "What is Event Thread?" video. Have you watched it?

[Edit}: Oops, it doesn't contain what I hoped it should. Anyway - here is a great explanation of how to process some of your stuff on Event Thread by Peter Strange:

Global Dialog - show when application is in background

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 102
Registered: ‎07-26-2011
My Device: Not a blackberry user
My Carrier: None

Re: Thread synchronization

[ Edited ]

 

Greetings.

 

As arkadyz said, when your call to animCompleted() is executed, if animCompleted boolean variable is false it wouldn't take us unaware.

Your call to animCompleted() must be done from within your overriden run method of your slide thread inner class. Further, you must call it in such a way that the event thread be who modifies the UI and not other foreign thread.

 

In order to achieve that, you must do something like this:

 

 

private synchronized void animateProfile() {
        Thread slide = new Thread() {
            public void run() {
                int count = 0;
                int height = profileClick.getPreferredHeight() + 4;
                while (count < height) {
                    synchronized (UiApplication.getEventLock()) {
                    	profileClick.moveScreen(0, count);
                        invalidate();
					}
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count += 8;
                }
                UiApplication.getUiApplication().invokeLater(new Runnable(){
					public void run() {
						profileClick.setFocus();
					}
                	
                });
            }
        };
        slide.start();
}

 

Let me know how useful this was for you, please. Good luck.


Let me know if this was helpful or if you have a solution.

Johan Guzmán - In case my answer pleased you, give a press to the LIKE button, please!! Mark this post as solution if it solved your problem.
Developer
Posts: 91
Registered: ‎06-21-2011
My Device: Blackberry Torch 9800
My Carrier: developer

Re: Thread synchronization

Thanks a lot Johanguzman!

Regards,
Arsen
Developer
Posts: 91
Registered: ‎06-21-2011
My Device: Blackberry Torch 9800
My Carrier: developer

Re: Thread synchronization

Thanks arkadyz it was very useful.

Regards,
Arsen
Developer
Posts: 102
Registered: ‎07-26-2011
My Device: Not a blackberry user
My Carrier: None

Re: Thread synchronization

[ Edited ]

Completely my pleasure, Arsen. I'm glad for helping.

Good luck at all!!


Let me know if this was helpful or if you have a solution.

Johan Guzmán - In case my answer pleased you, give a press to the LIKE button, please!! Mark this post as solution if it solved your problem.