黑莓信号量类

发布于 2024-08-08 23:17:49 字数 102 浏览 3 评论 0原文

我似乎在 Blackberry Java Reference 中找不到任何与信号量等效的东西。我缺少什么? java.util.concurrent 甚至不存在。

谢谢!肖恩

I can't seem to find anything equivalent to a Semaphore in the Blackberry Java Reference. What am I missing? java.util.concurrent isn't even there.

Thanks! Sean

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-08-15 23:17:50

来自 Eric Giguere 的 在 J2ME 应用程序中使用线程

监视器维护一个等待线程队列,一次只允许一个线程进入块。

由于每个 Java 对象都可以充当监视器,因此 java.lang.Object 类定义了三个公开此基本功能的方法:wait()、notify() 和 notifyAll()。任何线程都可以通过调用对象的 wait() 方法来挂起自身:

...
Object obj = .... // some object to use as a lock

synchronized( obj ){
    // here is where you'd check obj's state

    try {
    obj.wait();
    }
    catch( InterruptedException e ){
    }
}
...

线程必须在调用对象的 wait() 方法之前锁定该对象。它还必须捕获 java.lang.InterruptedException 并适当处理线程中断。线程在挂起自身后隐式释放对象上的锁。

wait()方法被重载:如果线程不想无限期地等待,它可以指定一个可选的超时(以毫秒为单位)。

一旦一个线程挂起自身,另一个线程就会通过调用同一对象的notify()或notifyAll()方法来释放它:

...
Object obj = .... // same object used as lock!

synchronized( obj ){
    obj.notify();  // or notifyAll
}
...

同样,第二个线程必须在调用notify() 或notifyAll() 之前锁定该对象。这两种方法的行为相同,只是一种方法唤醒一个等待线程,而另一种方法唤醒所有等待线程。线程被唤醒的顺序没有指定。每个新唤醒的线程必须重新获取对象上的锁,然后才能实际继续,因为当它挂起自身时,它在对象上的锁已隐式释放。

From Using Threads in J2ME Applications by Eric Giguere :

The monitor maintains a queue of waiting threads, allowing only one thread at a time to enter the block.

Because every Java object can act as a monitor, the java.lang.Object class defines three methods that expose this basic functionality: wait(), notify(), and notifyAll(). Any thread can suspend itself by calling an object's wait() method:

...
Object obj = .... // some object to use as a lock

synchronized( obj ){
    // here is where you'd check obj's state

    try {
    obj.wait();
    }
    catch( InterruptedException e ){
    }
}
...

The thread must lock the object before invoking its wait() method. It must also catch the java.lang.InterruptedException and deal appropriately with thread interruptions. The thread implicitly releases its lock on the object after it suspends itself.

The wait() method is overloaded: The thread can specify an optional timeout in milliseconds if it doesn't want to wait indefinitely.

Once a thread suspends itself, another thread releases it by invoking the same object's notify() or notifyAll() method:

...
Object obj = .... // same object used as lock!

synchronized( obj ){
    obj.notify();  // or notifyAll
}
...

Again, the second thread must lock the object before calling notify() or notifyAll(). These two methods behave the same except that one wakes one waiting thread while the other wakes all waiting threads. The order in which threads are woken is not specified. Each newly woken thread must re-obtain its lock on the object before it can actually proceed, because its lock on the object was implicitly released when it suspended itself.

七月上 2024-08-15 23:17:49

因此,对于遇到此问题的任何人,coldice 的出色回答将我带到此页面,该页面解释了如何使用 J2ME 中提供的等待/通知原语来实现各种类型的信号量 http://tutorials.jenkov.com/java-concurrency/semaphores.html。谢谢!

So for anyone who comes across this, coldice' excellent answer lead me to this page, which explains how to implement various types of Semaphores using the wait/notify primitives available in J2ME http://tutorials.jenkov.com/java-concurrency/semaphores.html. Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文