如何使用信号量同时执行2个线程

how to execute 2 threads in the same time using semaphore

我为2列火车创建了2个线程,第一个是(nord-sud)方向,第二个是(sud-nord)方向,所以只有一条铁路,不能满足2个不同方向的火车。 . 所以这是我的主要 class :

 public static void main(String[] args) throws InterruptedException {        
        Semaphore sem = new Semaphore(1); 
 
        MyThread train1 = new MyThread(sem, "nord-sud"); 
        MyThread train2 = new MyThread(sem, "sud-nord"); 
         
        // stating threads 1 and train 2 
        train1.start(); 
        train2.start(); 
        train1.join();       
        train2.join();
    }

这是我的 MyThread class 扩展线程 class :

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("nord-sud")) 
        { 
            System.out.println("Starting " + threadName); 
            try 
            { 
                System.out.println(threadName + " wait for railway to enter"); 
              
                // acquiring the lock 
                sem.acquire(); 
              
                System.out.println(threadName + " have access to enter"); 
          
                // 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 + ": " + i); 
          
                    // Now, allowing a context switch -- if possible. 
                    // for thread B to execute 
                    Thread.sleep(1000); 
                } 
            } catch (InterruptedException exc) { 
                    System.out.println(exc); 
                } 
          
                // Release the permit. 
                
                System.out.println(threadName + " left the railway "); 
                sem.release(); 
                
                
        } 
          
        // run by thread B 
        else
        { 
            System.out.println("Starting " + threadName); 
            try 
            { 
                // First, get a permit. 
                System.out.println(threadName + " wait for railway to enter"); 
              
          
                sem.acquire(); 
              
                System.out.println(threadName + " have access to enter"); 
          
                for(int i=0; i < 5; i++) 
                { 
                    Shared.count--; 
                    System.out.println(threadName + ": " + i); 
          
              
                    Thread.sleep(1000); 
                } 
            } catch (InterruptedException exc) { 
                    System.out.println(exc); 
                } 
                // Release the permit. 
                System.out.println(threadName + "left the railway"); 
                sem.release();
        } 
    } 
   
}

所以当我在同一时期添加 2 列南北方向的火车时,我希望他们都进入铁路,因为他们有相同的方向,只有当他意识到另一列火车时,火车才应该等待不同的方向是在铁路。那么我怎样才能同时执行 2 个线程呢?例如 train1(nord-sud)、train2(nord-sud) 和 train3(sud-nord)。所以火车 1 和火车 2 都进入铁路,火车 3 等他们离开才能进入。

在这种情况下,您应该为 nord-sud 和 sud-nord 列车分别建立一个列表和一个线程。列表线程应该负责获取信号量,然后在获取信号量时它们应该启动单独的火车线程(大概在第一个之后为每个线程添加延迟,这样火车就不会同时运行)。然后,列表线程应在每个火车线程上调用 Thread.join()(尽管实际上可能只需要在最后一个线程上加入),然后在最后一列火车到达后释放信号量。

您可以为方向相似的列车创建两个权限,为不同方向的列车创建一个权限,例如:

public class Solution0 {
public static void main(String[] args) throws InterruptedException {
    String direction1 = "nord-sud";
    String direction2 = "sud-sud";
    int permissionCount;
    if (direction1.equals(direction2)){
        permissionCount = 2;
    }else {
        permissionCount = 1;
    }
    Semaphore sem = new Semaphore(permissionCount);

    MyThread train1 = new MyThread(sem, direction1);
    MyThread train2 = new MyThread(sem, direction2);

    // stating threads 1 and train 2
    train1.start();
    train2.start();
    train1.join();
    train2.join();
}

}

不同方向的输出:

Starting sud-sud
Starting nord-sud
sud-sud wait for railway to enter
nord-sud wait for railway to enter
sud-sud have access to enter
sud-sud: 0
sud-sud: 1
sud-sud: 2
sud-sud: 3
sud-sud: 4
sud-sudleft the railway
nord-sud have access to enter
nord-sud: 0
nord-sud: 1
nord-sud: 2
nord-sud: 3
nord-sud: 4
nord-sud left the railway

类似方向的输出:

Starting sud-sud
Starting sud-sud
sud-sud wait for railway to enter
sud-sud wait for railway to enter
sud-sud have access to enter
sud-sud have access to enter
sud-sud: 0
sud-sud: 0
sud-sud: 1
sud-sud: 1
sud-sud: 2
sud-sud: 2
sud-sud: 3
sud-sud: 3
sud-sud: 4
sud-sud: 4
sud-sudleft the railway
sud-sudleft the railway