我创建了一个联网的停车场系统,其中包含两个入口客户端,两个出口客户端和一个服务器。如果停车场没有空间,我的目的是让汽车在入口处排队。
这是我到目前为止所做的:
import javax.swing.JFrame;
import javax.swing.JOptionPane;
@SuppressWarnings("unused")
public class SharedState extends Thread{
private SharedState mySharedObj;
private String myThreadName;
private int totalSpaces;
private int groundSpaces;
private int firstSpaces;
private boolean accessing=false; // true a thread has a lock, false otherwise
private int threadsWaiting = 0; // number of waiting writers
JFrame Notification = new JFrame();
// Constructor
SharedState(int groundFloor, int firstFloor) {
groundSpaces = groundFloor;
firstSpaces = firstFloor;
}
//Attempt to aquire a lock
public synchronized void acquireLock() throws InterruptedException{
Thread me = Thread.currentThread(); // get a ref to the current thread
++threadsWaiting;
while (accessing) { // while someone else is accessing or threadsWaiting > 0
//wait for the lock to be released - see releaseLock() below
wait();
}
// nobody has got a lock so get one
--threadsWaiting;
accessing = true;
}
// Releases a lock to when a thread is finished
public synchronized void releaseLock() {
//release the lock and tell everyone
accessing = false;
notifyAll();
Thread me = Thread.currentThread(); // get a ref to the current thread
}
public synchronized String processInput(String myThreadName, String theInput) throws InterruptedException
{
String theOutput = null;
// Check what the client said
if (theInput != null)
{
//Correct request
if(myThreadName.equals("GroundFloorEntrance"))
{
if(groundSpaces > 0)
{
groundSpaces--;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle arrived.\nGround floor spaces = " + groundSpaces + "\nTotal spaces = " + totalSpaces;
}
else if (firstSpaces > 0)
{
firstSpaces--;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle arrived.\nFirst floor spaces = " + firstSpaces + "\nTotal spaces = " + totalSpaces;
}
else
{
theOutput = "No spaces available, please queue.";
}
}
if(myThreadName.equals("FirstFloorEntrance"))
{
if(firstSpaces > 0)
{
firstSpaces--;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle arrived.\nFirst floor spaces = " + firstSpaces + "\nTotal spaces = " + totalSpaces;
}
else if (groundSpaces > 0)
{
groundSpaces--;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle arrived.\nGround floor spaces = " + groundSpaces + "\nTotal spaces = " + totalSpaces;
}
else
{
theOutput = "No spaces available, please queue.";
}
}
if(myThreadName.equals("GroundFloorExit1"))
{
if(groundSpaces < 20)
{
groundSpaces++;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle departed.\nGround floor spaces = " + groundSpaces + "\nTotal spaces = " + totalSpaces;
}
else
{
theOutput = "Vehicle departed.\nGround floor spaces = " + groundSpaces + "\nTotal spaces = " + totalSpaces;
}
}
if(myThreadName.equals("GroundFloorExit2"))
{
if(firstSpaces < 20)
{
firstSpaces++;
totalSpaces = groundSpaces + firstSpaces;
theOutput = "Vehicle departed.\nFirst floor spaces = " + firstSpaces + "\nTotal spaces = " + totalSpaces;
}
else
{
theOutput = "Vehicle departed.\nFirst floor spaces = " + firstSpaces + "\nTotal spaces = " + totalSpaces;
}
}
}
//Return the output message to the Server
JOptionPane.showMessageDialog(Notification, theOutput);
System.out.println("\n" + theOutput);
return theOutput;
}
}
我希望汽车在没有可用空间时排队,并在汽车退出时自动运行。我相信当其中一个退出客户端运行时,我需要有效地将线程排队以在IF语句中运行。但是,我不确定我会怎么做。
答案 0 :(得分:0)
使用这种机制。在你的项目中以适当的方式使用这种机制。初始添加所有可用的停车位到队列
public static BlockingQueue blockingQueue = new ArrayBlockingQueue<Integer>(10);
public static void main(String[] args) {
Thread t1 = new Thread(()-> {
while(true){
Random random = new Random();
try {
int i = random.nextInt(10);
if(i!=8) { // a car request parking space
System.out.println("waiting until parking available");
int j = (int) blockingQueue.take();
System.out.println("released parking space "+j+" to requested car " );
}
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread t2 = new Thread(()->{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while(true){
Random random = new Random();
try {
int i = random.nextInt(10);
if(i!=5) { //car went out from parking space .add avilable slot to queue
System.out.println("add free parking space:" + i +" to queue");
blockingQueue.put(i);
}
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
t2.start();
}