【发布时间】:2018-04-17 05:03:27
【问题描述】:
我创建了一个网络停车场系统,其中包含两个入口客户端、两个出口客户端和一个服务器。我的目标是在停车场没有空位时让汽车在入口处排队。
这是我到目前为止所做的:
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 语句中运行。但是,我不确定该怎么做。
【问题讨论】:
-
看看 BlockingQueus/Deques docs.oracle.com/javase/7/docs/api/index.html?java/util/…。我认为带有条件的 java.util.concurrent.locks.Lock 也可以适合您的解决方案。
-
或者如果你想在线程级别工作stackoverflow.com/questions/6306132/…,也可以使用有界队列的线程池。
标签: java multithreading