Just to add my two cents:

You didn't include information on what s_Semaphore is, but I'm guessing it's from java.concurrent.

Although I'm not too familiar with this package, the docs say this:

http://download.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/Semaphore.html

Conceptually, a semaphore maintains a set of permits. Each acquire() blocks if necessary until a permit is available, and then takes it.

If you constructed the semaphore with "1" for "permits", then what you are seeing is as designed - the semaphore will block on second acquire. It's not reentrant, the way synchronized blocks are (which can be owned multiple times by the same thread).

You can verify this with a simple test:

        try {
            Semaphore sem = new Semaphore(1);
            Log.i(TAG, "Semaphore constructed");
            sem.acquire();
            Log.i(TAG, "Semaphore acquired once");
            sem.acquire();
            Log.i(TAG, "Semaphore acquired again");
        } catch (InterruptedException x) {
            Log.e(TAG, "Semaphore interrupted: ", x);
        }

The test above will block on second acquire, and that is correct behavior, since the semaphore's permit count is 1.

Compare this with synchronized keyword:

        synchronized (this) {
            Log.i(TAG, "This is synchronized block 1");
            synchronized (this) {
                Log.i(TAG, "This is synchronized block 2");
            }
        }

The above will execute both blocks.

A few ways to fix the problem:

- Use a CyclicBarrier instead of a Semaphore.

- Rewrite the code using only the language's built-in synchronization syntax:

int queryUserSave()
{
  // start the activity, then
    synchronized(this) { wait();  }
    return s_nQueryUserSaveResult;
}

void onQueryUserSaveResult(int aResult)
{
    synchronized (this) {
      s_nQueryDialingLineResult = aResult;
      notify();
    }
}

-- Kostya

22.12.2010 20:58, Dianne Hackborn ?????:
startActivity() always runs the activity on the main thread.

How about just looking at your app in the debugging to see where the threads are stuck?

On Wed, Dec 22, 2010 at 7:11 AM, Schoel <[email protected] <mailto:[email protected]>> wrote:

    I have a function call from a large engine (which I can not change)
    which asks the user whether to save or not. Before this function call
    returns, I need an answer from the user whether to save.

    There is, as far as I know, no way to start an activity (or show a
    dialog) that synchronously returns an answer. Therefore I did like
    this (code is not complete but should be enough for understanding):

    int queryUserSave()
    {
      s_Semaphore.acquire();
      startActivity(QueryUserSaveActivity);
      s_Semaphore.acquire();
      s_Semaphore.release();
      return s_nQueryUserSaveResult;
    }

    void onQueryUserSaveResult(int aResult)
    {
      s_nQueryDialingLineResult = aResult;
      s_Semaphore.release();
    }


    The problem is, on the second acquire, the program halts (as intended)
    without spawning the QueryUserSaveActivity (not as intended). The idea
    was that the QueryUserSaveActivity would be spawned and the second
    acquire would block until onQueryUserSaveResult is called.
    I've checked the thread id of the thread in which queryUserSave is
    called and the thread id in which the QueryUserSaveActivity is started
    (if I don't use any semaphores) and they are not the same. I thus
    conclude that startActivity starts the activity in another thread (the
    UI thread) and therefore shouldn't care about the second acquire call.
    I also tried setting the startActivity in its own thread and start
    that thread with the same result as above.
    Do you have any ideas what I can do to solve this?

    Appriciate any help,
    Schoel

    --
    You received this message because you are subscribed to the Google
    Groups "Android Developers" group.
    To post to this group, send email to
    [email protected]
    <mailto:[email protected]>
    To unsubscribe from this group, send email to
    [email protected]
    <mailto:android-developers%[email protected]>
    For more options, visit this group at
    http://groups.google.com/group/android-developers?hl=en




--
Dianne Hackborn
Android framework engineer
[email protected] <mailto:[email protected]>

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to