我是Java新手,想了解同步是如何工作的。所以我創建了一個Plane Reservation System,我可以模擬多個用戶嘗試預訂並使用Synchromization來獲取正確的輸出。Java - 多用戶應用程序中的同步
現在,它的工作原理,我想這是如何在現實世界中的作品。例如,讓我們說應用程序是建立在Swing上的,這是一個多用戶應用程序。爲了簡單起見,我們假設只有兩架飛機'AAA'和'BBB'。該應用程序可以安裝在售票員代理人的計算機,機場的售貨亭以及不同旅行社的計算機中,並且他們都可以訪問相同的數據庫。
在這種情況下,每個用戶/計算機都有自己的Reserve類,Transaction類和Plane類的實例。所以Transaction類中只有一個線程/請求,並且沒有同步。
我的問題是,像這個Reservation系統這樣的多用戶應用程序將如何設計/實現,以便所有用戶都訪問Transacion類的一個實例,以便可以進行同步。你也可以看看這個問題,我怎樣才能建立不同計算機上不同玩家玩的多用戶遊戲。再舉一個例子,銀行系統可以在ATM和櫃員機上運行應用程序時進行存款,取款和轉賬。
///////////////////////////////
Reserve.java --->項點由用戶
/////////////////////////////
import java.io.IOException;
public class Reserve {
static int queryseatsavailableinx;
static int queryseatsavailableiny;
static
{
seats s = null;
try {
s = new seats();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
queryseatsavailableinx = s.getseatsinplanex("AAA");
queryseatsavailableiny = s.getseatsinplanex("BBB");
} catch (IOException e) {
e.printStackTrace();
}
}
static final Plane x1 = new Plane("AAA", 001, queryseatsavailableinx);
static final Plane y1 = new Plane("BBB", 002, queryseatsavailableiny);
static final Transaction trans = new Transaction();
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
public void run() {
trans.getPlaneInfo(x1);
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
//trans.getPlaneInfo(x1);
trans.reserveSeats(x1,3);
}
});
Thread t3 = new Thread(new Runnable() {
public void run() {
//trans.getPlaneInfo(y1);
trans.reserveSeats(y1,8);
}
});
Thread t4 = new Thread(new Runnable() {
public void run() {
//trans.getPlaneInfo(x1);
trans.reserveSeats(x1,2);
}
});
t1.start();
t2.start();
t3.start();
t4.start();
t1.join();
t2.join();
t3.join();
t4.join();
}
}
由每個請求//// ///////////////////////////
Transaction.java --->實際交易發生在這裏
/////////////////////////////
public class Transaction {
public void getPlaneInfo(Plane x){
synchronized(this){
int number = x.getSeatCapacity();
String planename=x.getPlaneName();
System.out.printf("The number of seats in plane %s is %d\n",planename,number);
}
}
public void reserveSeats(Plane x, int seatstobereserved) {
synchronized(this){
x.updateSeatCapacity(seatstobereserved);
}
}
}
//////////// ///////////////////
Plane.java --->的有關飛機
信息//////////// /////////////////
final public class Plane {
private String planename = null;
private int planeid = 0;
private int availableseatcapacity = 0;
Plane(String planename, int planeid, int seatcapacity) {
this.planename = planename;
this.planeid = planeid;
this.availableseatcapacity = seatcapacity;
}
public String getPlaneName() {
return planename;
}
public int getPlaneId() {
return planeid;
}
public int getSeatCapacity() {
return availableseatcapacity;
}
public void updateSeatCapacity(int reservedseats) {
availableseatcapacity -= reservedseats;
System.out.printf("\n%d Seats successfully reserved and remaining seats " +
"in the plane %s are %d\n",reservedseats, planename,availableseatcapacity);
}
}
//////////////////////// /////
Seats.java --->爲了模擬用戶每次發出請求時,從一個公共數據源/數據庫中檢索'可用位置數'。 /////////////////////////////
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
final public class seats {
seats() throws IOException {
DataOutputStream di1 = new DataOutputStream(new FileOutputStream(
"\\PlaneReservation\\bin\\a.bin"));
DataOutputStream di2 = new DataOutputStream(new FileOutputStream(
"\\PlaneReservation\\bin\\b.bin"));
di1.writeInt(300);
di2.writeInt(200);
di1.flush();
di2.flush();
di1.close();
di2.close();
}
public int getseatsinplanex(String s) throws IOException {
if (s.equals("AAA")) {
FileInputStream fis1 = new FileInputStream("\\PlaneReservation\\bin\\a.bin");
DataInputStream dis1 = new DataInputStream(fis1);
int number = 0;
boolean eof = false;
while (!eof) {
try {
number = dis1.readInt();
// System.out.println(number);
} catch (EOFException eofx) {
eof = true;
dis1.close();
}
}
return number;
} else if (s.equals("BBB")) {
FileInputStream fis2 = new FileInputStream("\\PlaneReservation\\bin\\b.bin");
DataInputStream dis2 = new DataInputStream(fis2);
int number = 0;
boolean eof = false;
while (!eof) {
try {
number = dis2.readInt();
//System.out.println(number);
} catch (EOFException eofx) {
eof = true;
dis2.close();
}
}
return number;
}
return 0;
}
}
http://sscce.org/ – dantuch