信号量和同步块

问题描述

我创建了信号量的自定义子类。此子类别扩展了原始信号量,并且只有两种方法

1。将许可计数设置为零。

    public synchronized void resetPermits() {
        while (this.hasQueuedThreads()) {
            this.release(); // this --> Semaphore sublass
        }
        this.reducePermits(this.availablePermits());
    }

2。可用的许可证检查

    public synchronized boolean checkPermits() {
        return this.availablePermits() > 0;
    }

例如,我们有2个许可证。 假设 thread1 询问是否this.hasQueuedThreads()并进入睡眠状态(结果为true)。现在 thread2 正在工作。它执行整个方法。没有更多的权限了。第一个线程唤醒并继续执行。

我发现: JavaScript file upload size validation

还有: Why does the acquire() method in Semaphores not have to be synchronized?

据我了解,只有原始的信号量方法是线程安全的。

所以,我的问题是: 我的两个自定义方法都应该同步吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)