×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    How to wait CancellableTask to finish?

    Hello guys,

    I use WaitScreen component from NetBeans MIDP library. It uses CancellableTask which extends Runnable that I believe uses separate thread. My question is how to make the other threads to wait until this WaitScreen finish its process?
    This is the source code of WaitScreen:
    http://www.java2s.com/Open-Source/Ja...creen.java.htm
    And the description:
    http://netbeans.org/kb/docs/javame/waitscreen.html

    Thanks in advance.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to wait CancellableTask to finish?

    I have never used this API, someone else might have a better answer!

    On completing the Task, the WaitScreen will invoke the commandAction event of it's CommandListener, with either WaitScreen.SUCCESS_COMMAND or WaitScreen.FAILURE_COMMAND as the argument to the Command parameter. You could use this to awaken sleeping threads.

    Code:
    // somewhere accessible to all
    public static Object semaphore = new Object();
    Code:
    // in each thread that must wait
    synchronized (semaphore) {
        boolean woken = false;
        while (!woken) {
            try {
                // go to sleep, until we're woken up
                semaphore.wait();
                woken = true;
            } catch (InterruptedException ie) {
                // "woken" remains false
            }
        }
    }
    Code:
    // in the WaitScreen's CommandListener
    public void commandAction(Command c, Displayable d) {
        // you can check "d" to determine which WaitScreen just finished
    
        // you can check "c" to determine success or failure
    
        synchronize (semaphore) {
            // wake up all waiting threads
            semaphore.notifyAll();
        }
    }
    Does that help?

    Graham.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    3

    Re: How to wait CancellableTask to finish?

    Thanks Graham. It really works well. I am beginner to the thread concept and now I learn new thing. I really appreciate your help.

    Regards,

    Kosumo

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: How to wait CancellableTask to finish?

    Be careful with multi-threading. It is hard enough to write solid code with only one thing happening at at time. Each time you add a thread, you make the program more and more complex.

    Also, different phones might use different algorithms for deciding which thread to run. That means your application might run differently on different devices, if you are not careful. Remember that phones don't necessarily have sophisticated operating systems or multi-core processors, so multi-threading will not work as smoothly on a phone as it does on your PC.

    That said, multi-threading is essential to MIDP development. Event-handler methods must return as quickly as possible, so Threads are essential for running time-consuming actions like network access or resource loading.

    Have fun!

    Graham.

Similar Threads

  1. wait note problem.
    By abhi560 in forum Symbian
    Replies: 2
    Last Post: 2009-07-28, 04:42
  2. [announce] wait dialog wrapper 0.2
    By lfd in forum Symbian
    Replies: 10
    Last Post: 2008-03-21, 11:03
  3. How to wait for the Container to finish ConstructL?
    By strike_noir in forum Symbian User Interface
    Replies: 2
    Last Post: 2007-08-31, 12:29
  4. How To Incorporate A Wait Note With A BlueTooth Service Discovery?
    By Mo7ammed_01 in forum Bluetooth Technology
    Replies: 2
    Last Post: 2006-07-14, 08:22
  5. Replies: 4
    Last Post: 2005-11-10, 06:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×