gpt4 book ai didi

java - 我可以在 RESTful Web 服务中使用 wait() 吗?

转载 作者:行者123 更新时间:2023-12-02 03:03:16 25 4
gpt4 key购买 nike

我有一个 RESTful Web 服务,用于 NetBeans 上的服务器。该网络服务应该会收到来自客户端的许多请求(多人游戏)。

我对这个主题还是陌生的,但如果我理解正确的话 - 从客户端到我的 Web 服务的每次调用都是线程安全的 - 因为到 Web 服务的每个连接都在不同的线程上(我的所有变量都在 Web 服务内部)方法)这是真的吗?

这引出了我的问题:我可以在 Web 服务方法中使用 wait(); 吗?假设我正在等待两个客户端连接,因此第二个连接将使用 notifyAll();但由于网络服务并不是真正的线程,我不知道是否可以在那里使用这些方法?我应该用什么来代替??

这是我的网络服务:

@Path("/w")
public class JSONRESTService {
String returned;

@POST
@Consumes("application/json")
@Path("/JSONService")
public String JSONREST(InputStream incomingData) {
StringBuilder JSONBuilder = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(incomingData));
String line = null;
while ((line = in.readLine()) != null) {
JSONBuilder.append(line);
}

returned = "transfer was completed";

// This is what I'm trying to add but I know that I can't:

// count is a static variable, every new connection will increase this value

// only one player is connected
if (Utility.count == 1)
wait (); //wait for a 2nd player to connect to this webservice

// 2nd player is connected to this webservice
if (Utility.count == 2)
notifyAll (); // notify the 1st player

} catch (Exception e) {
System.out.println ("Error Parsing: - ");
returned ="error";
}
System.out.println ("Data Received: " + JSONBuilder.toString ());
return (returned);
}
}

客户端:

JSONObject jsonObject = new JSONObject("string");

// Step2: Now pass JSON File Data to REST Service
try {
URL url = new URL("http://localhost:8080/w/JSONService");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(jsonObject.toString());
out.close();

//string answer from server:
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while ((line = in.readLine()) != null) {
sb.append(line);
System.out.println("\n"+line);
in.close();
} catch (Exception e) {
System.out.println("\nError while calling JSON REST Service");
System.out.println(e);
}

br.close();
} catch (Exception e) {
e.printStackTrace();
} } }`

最佳答案

您始终可以使用wait()notify(),因为它会影响代码正在运行的线程。是否应该使用它取决于具体情况。

如果您想要玩家队列,请使用队列:)

我举出的一个小例子......

@Path("/w")
public class JSONRESTService {

private static BlockingQueue<Player> queue = new ArrayBlockingQueue<>(999);

@POST
@Consumes("application/json")
@Path("/JSONService")
public String JSONREST(InputStream incomingData) {


Player thisPlayer = ...; // Get player from session or something

System.out.println (thisPlayer.getName() + " starting...");

try {

if (queue.isEmpty()) {
System.out.println ("waiting for an opponent");
queue.add(thisPlayer);
synchronized (thisPlayer) {
thisPlayer.wait();
}
} else {
System.out.println ("get next in queue");
Player opponent = queue.take();
opponent.setOpponent(thisPlayer);
thisPlayer.setOpponent(opponent);
synchronized (opponent) {
opponent.notify();
}
}

System.out.println (thisPlayer.getName() + " playing " + thisPlayer.getOpponent().getName());

} catch (Exception ex) {
ex.printStackTrace();
}
}

static class Player {

private String name;
private Player opponent;

Player (String name) {
this.name = name;
}

public String getName() {
return name;
}

public Player getOpponent() {
return opponent;
}

public void setOpponent(Player opponent) {
this.opponent = opponent;
}
}
}

关于java - 我可以在 RESTful Web 服务中使用 wait() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42147628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com