📜  Java中的信号量

📅  最后修改于: 2022-05-13 01:54:42.753000             🧑  作者: Mango

Java中的信号量

信号量通过使用计数器来控制对共享资源的访问。如果计数器大于零,则允许访问。如果为零,则拒绝访问。计数器计数的是允许访问共享资源的许可。因此,要访问资源,线程必须从信号量中获得许可。

信号量的工作

通常,要使用信号量,想要访问共享资源的线程会尝试获取许可。

  • 如果信号量的计数大于零,则线程获得许可,这会导致信号量的计数减少。
  • 否则,线程将被阻塞,直到获得许可为止。
  • 当线程不再需要访问共享资源时,它会释放许可,这会导致信号量的计数增加。
  • 如果有另一个线程在等待许可,则该线程将在那时获得许可。

Java在实现此机制的Java.util.concurrent包中提供了Semaphore类,因此您不必实现自己的信号量。

流程图 :
d

Semaphore 类中的构造函数: Semaphore 类中有两个构造函数。

Semaphore(int num)
Semaphore(int num, boolean how)

这里, num指定初始许可计数。因此,它指定了任何时候可以访问共享资源的线程数。如果是 1,那么任何时候只有一个线程可以访问资源。默认情况下,所有等待的线程都以未定义的顺序被授予许可。通过将how设置为 true,您可以确保等待线程按照它们请求访问的顺序被授予许可。

使用信号量作为锁(防止竞争条件)

我们可以使用信号量来锁定对资源的访问,每个想要使用该资源的线程必须首先调用acquire() ,然后才能访问资源以获取锁。当线程用完资源后,它必须调用release()来释放锁。下面是一个例子来证明这一点:

// java program to demonstrate 
// use of semaphores Locks
import java.util.concurrent.*;
  
//A shared resource/class.
class Shared 
{
    static int count = 0;
}
  
class MyThread extends Thread
{
    Semaphore sem;
    String threadName;
    public MyThread(Semaphore sem, String threadName) 
    {
        super(threadName);
        this.sem = sem;
        this.threadName = threadName;
    }
  
    @Override
    public void run() {
          
        // run by thread A
        if(this.getName().equals("A"))
        {
            System.out.println("Starting " + threadName);
            try 
            {
                // First, get a permit.
                System.out.println(threadName + " is waiting for a permit.");
              
                // acquiring the lock
                sem.acquire();
              
                System.out.println(threadName + " gets a permit.");
          
                // Now, accessing the shared resource.
                // other waiting threads will wait, until this 
                // thread release the lock
                for(int i=0; i < 5; i++)
                {
                    Shared.count++;
                    System.out.println(threadName + ": " + Shared.count);
          
                    // Now, allowing a context switch -- if possible.
                    // for thread B to execute
                    Thread.sleep(10);
                }
            } catch (InterruptedException exc) {
                    System.out.println(exc);
                }
          
                // Release the permit.
                System.out.println(threadName + " releases the permit.");
                sem.release();
        }
          
        // run by thread B
        else
        {
            System.out.println("Starting " + threadName);
            try 
            {
                // First, get a permit.
                System.out.println(threadName + " is waiting for a permit.");
              
                // acquiring the lock
                sem.acquire();
              
                System.out.println(threadName + " gets a permit.");
          
                // Now, accessing the shared resource.
                // other waiting threads will wait, until this 
                // thread release the lock
                for(int i=0; i < 5; i++)
                {
                    Shared.count--;
                    System.out.println(threadName + ": " + Shared.count);
          
                    // Now, allowing a context switch -- if possible.
                    // for thread A to execute
                    Thread.sleep(10);
                }
            } catch (InterruptedException exc) {
                    System.out.println(exc);
                }
                // Release the permit.
                System.out.println(threadName + " releases the permit.");
                sem.release();
        }
    }
}
  
// Driver class
public class SemaphoreDemo 
{
    public static void main(String args[]) throws InterruptedException 
    {
        // creating a Semaphore object
        // with number of permits 1
        Semaphore sem = new Semaphore(1);
          
        // creating two threads with name A and B
        // Note that thread A will increment the count
        // and thread B will decrement the count
        MyThread mt1 = new MyThread(sem, "A");
        MyThread mt2 = new MyThread(sem, "B");
          
        // stating threads A and B
        mt1.start();
        mt2.start();
          
        // waiting for threads A and B 
        mt1.join();
        mt2.join();
          
        // count will always remain 0 after
        // both threads will complete their execution
        System.out.println("count: " + Shared.count);
    }
}

输出:

Starting A
Starting B
A is waiting for a permit.
B is waiting for a permit.
A gets a permit.
A: 1
A: 2
A: 3
A: 4
A: 5
A releases the permit.
B gets a permit.
B: 4
B: 3
B: 2
B: 1
B: 0
B releases the permit.
count: 0

注意:上述程序的不同执行输出可能不同,但计数变量的最终值将始终保持为 0。

上述程序说明:

  • 该程序使用信号量来控制对count变量的访问,该变量是 Shared 类中的静态变量。 Shared.count由线程 A 递增 5 次,由线程 B 递减 5 次。为了防止这两个线程同时访问 Shared.count,只有在从控制信号量获得许可后才允许访问。访问完成后,许可被释放。这样,一次只有一个线程可以访问 Shared.count,如输出所示。
  • 注意在 MyThread 类的 run() 方法中对 sleep() 的调用。它用于“证明”对 Shared.count 的访问是由信号量同步的。在 run() 中,对 sleep() 的调用会导致调用线程在每次访问 Shared.count 之间暂停。这通常会使第二个线程运行。但是,由于信号量,第二个线程必须等到第一个线程释放许可,这仅在第一个线程的所有访问都完成后才会发生。因此,Shared.count 首先由线程 A 递增五次,然后由线程 B 递减五次。在汇编代码中递增和递减不会混合。
  • 如果不使用信号量,两个线程对Shared.count的访问将同时发生,并且增量和减量将混合在一起。要确认这一点,请尝试注释掉对acquire( )release( )的调用。运行程序时,您会看到对 Shared.count 的访问不再同步,因此您不会总是得到数值 0。

下一篇: Java Java类