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



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

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类扩展Thread类:

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();
} 
} 

}

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

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

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

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

最新更新