使用java实现计数信号量

发布于 2024-08-19 07:42:14 字数 761 浏览 6 评论 0 原文

我怀疑互斥体和信号量之间的显着区别在于,计数信号量支持超过一次的最大访问,因为互斥体一次最多只支持一次访问。

但是当执行如下时;

public class countingSemaphore{
 private static final int _MOSTTABLES = 3;  // whatever maximum number
 private static int availtable = _MOSTTABLES;

 public synchronized static void Wait(){  
  while(availtable==0){  
   try{
    wait();    
   }
   catch(InterruptedException e){
    e.printStackTrace();
   }
  }
  availtable--;  
 }

 public synchronized static void Signal(){
  while(availtable==_MOSTTABLES){
   try{
    wait();
   }
   catch(InterruptedException e){
    e.printStackTrace();
   }
  }
  availtable++;  
 }
}

问题是调用对象的非静态 wait() 方法。但是,我必须将同步应用于类而不是对象的实例,因为访问是在多个实例之间共享的。

如何解决 wait() 错误?我们在java中是否有其他方法或者我们必须自己实现wait()?

I suspect the significant difference between mutex and semaphore is that counting semaphore supports maximum access more than one since mutext only supports at most one access at one time.

But when doing the implementation as follows;

public class countingSemaphore{
 private static final int _MOSTTABLES = 3;  // whatever maximum number
 private static int availtable = _MOSTTABLES;

 public synchronized static void Wait(){  
  while(availtable==0){  
   try{
    wait();    
   }
   catch(InterruptedException e){
    e.printStackTrace();
   }
  }
  availtable--;  
 }

 public synchronized static void Signal(){
  while(availtable==_MOSTTABLES){
   try{
    wait();
   }
   catch(InterruptedException e){
    e.printStackTrace();
   }
  }
  availtable++;  
 }
}

the problem is the calling of non-static wait() method of object. But, I have to apply synchronization to class instead of instances of objects since accessing is shared among multiple instances.

How to resolve the wait() error? Do we have another method in java or we have to implement wait() ourselves?

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

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

发布评论

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

评论(2

半夏半凉 2024-08-26 07:42:15

从概念上讲,您正在寻找一个信号量(用一个许可证初始化,其行为相当于互斥量)。

如果您无法使用 J2SE 5.0,那么我建议您查看它的前身 util.concurrent,它属于公共领域,可以在 J2SE 5.0 之前的 Java 版本上向后移植/使用(我也在有限的设备上使用了一些派生类)。

看一下 信号量 及其提供衍生类的顺序,例如 FIFO信号量

如果您需要书架的指导和参考,我推荐“Java 中的并发编程”< /a>,作者:Doug Lea,他负责 util.concurrent 以及为我们带来 java.util.concurrent 的 JSR。

Conceptually you are looking for a Semaphore (which initialized with one permit, behaves equivalent to a Mutex).

If you cannot use J2SE 5.0, then I would suggest to check out it's predecessor util.concurrent, which is in Public Domain and can be backported/used on Java versions before J2SE 5.0 (I have used some derived classes on limited devices as well).

Take a look at the Semaphore and it's order providing derivative classes, e.g. FIFOSemaphore.

If you need guidance and a reference for the bookshelf, I recommend "Concurrent Programming in Java", by Doug Lea, who was responsible for util.concurrent and the JSR that brought us java.util.concurrent.

南薇 2024-08-26 07:42:15

您在这里还有很多其他问题需要处理(如果您正在尝试实现并发控制对象),但是解决如何对静态级别构造使用等待/通知机制的特定问题的一种解决方案就是简单地理清这个概念构造中的监视器:您使用类的静态方法,但同步是在静态方法引用的对象(任何对象)的特定实例上执行的。例如:

public class MySemaphore {
   // ...
   private final Object lock = new Object();

   public static void acquire(int count) {
       while( ...) {
          synchronized(lock) {
              lock.wait();
          }
       }
   }
   public static void release(int count) {
       while( ...) {
          synchronized(lock) {
              lock.notifyAll();
          }
       }
   }
}

You have a great many other issues to deal with here (if you are trying to implement a concurrency control object) but one solution to your specific issue of how to use a wait/notify mechanism for a static level construct is simply to untangle the notion of a monitor from the construct: you use the static methods of your class but synchronization is performed on a specific instance of an object (any object) that is referenced by the static methods. For example:

public class MySemaphore {
   // ...
   private final Object lock = new Object();

   public static void acquire(int count) {
       while( ...) {
          synchronized(lock) {
              lock.wait();
          }
       }
   }
   public static void release(int count) {
       while( ...) {
          synchronized(lock) {
              lock.notifyAll();
          }
       }
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文