在java中放置Semaphore问题(placing Semaphore issue in java)
我实现了一个客户端,它向服务器发送随机数,等待服务器的重放(假设服务器也发送一些随机值)。 客户端在从服务器获得答案之前无法发送另一个请求。
这是我的代码:
private ObjectOutputStream output; private ObjectInputStream input; private String message = ""; private String serverIP; private Socket connection; private String clientName; private String queryNum; Semaphore waitForReplay ; //constructor public Client(String host,String clientName){ serverIP = host; waitForReplay = new Semaphore(1, true); this.clientName = clientName; startRunning(); } public void startRunning(){ try{ connectToServer(); setupStreams(); new Thread(){ public void run(){ try { whileChatting(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); sendMsgToChat(); }catch(EOFException eofException){ showMessage("\n Client terminated the connection"); }catch(IOException ioException){ ioException.printStackTrace(); }finally{ closeConnection(); } } //connect to server private void connectToServer() throws IOException{ showMessage("Attempting connection... \n"); connection = new Socket(InetAddress.getByName(serverIP), 6789); showMessage("Connection Established! Connected to: " + connection.getInetAddress().getHostName()); } //set up streams private void setupStreams() throws IOException{ output = new ObjectOutputStream(connection.getOutputStream()); output.flush(); input = new ObjectInputStream(connection.getInputStream()); showMessage("\n The streams are now set up! \n"); } //receiving msgs private void whileChatting() throws IOException{ do{ try{ message = (String) input.readObject(); showMessage("\nClient"+clientName+": got replay " + message+ " for query " +queryNum); }catch(ClassNotFoundException classNotFoundException){ showMessage("Unknown data received!"); } finally{ waitForReplay.release(); } }while(true); } //sending msgs private void sendMsgToChat(){ while(true){ try { waitForReplay.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } //sends some random num, casting int number from array to string sendMessage(queryNum =some random number..); } }
由于一个我无法找到的原因,waitForRepaly Semaphore不做他的工作,并且在某些情况下客户端连续发送几条消息,而不是等待来自服务器的回复。 你能帮我找到我代码中的错误吗?
i've implemented a client side which sends random numbers to the server, wait for a replay from the server (let's say the server also sends some random value). the client can't send another request before he gets an answer from the server.
here's my code:
private ObjectOutputStream output; private ObjectInputStream input; private String message = ""; private String serverIP; private Socket connection; private String clientName; private String queryNum; Semaphore waitForReplay ; //constructor public Client(String host,String clientName){ serverIP = host; waitForReplay = new Semaphore(1, true); this.clientName = clientName; startRunning(); } public void startRunning(){ try{ connectToServer(); setupStreams(); new Thread(){ public void run(){ try { whileChatting(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }.start(); sendMsgToChat(); }catch(EOFException eofException){ showMessage("\n Client terminated the connection"); }catch(IOException ioException){ ioException.printStackTrace(); }finally{ closeConnection(); } } //connect to server private void connectToServer() throws IOException{ showMessage("Attempting connection... \n"); connection = new Socket(InetAddress.getByName(serverIP), 6789); showMessage("Connection Established! Connected to: " + connection.getInetAddress().getHostName()); } //set up streams private void setupStreams() throws IOException{ output = new ObjectOutputStream(connection.getOutputStream()); output.flush(); input = new ObjectInputStream(connection.getInputStream()); showMessage("\n The streams are now set up! \n"); } //receiving msgs private void whileChatting() throws IOException{ do{ try{ message = (String) input.readObject(); showMessage("\nClient"+clientName+": got replay " + message+ " for query " +queryNum); }catch(ClassNotFoundException classNotFoundException){ showMessage("Unknown data received!"); } finally{ waitForReplay.release(); } }while(true); } //sending msgs private void sendMsgToChat(){ while(true){ try { waitForReplay.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } //sends some random num, casting int number from array to string sendMessage(queryNum =some random number..); } }
for a reason i can't find out, the waitForRepaly Semaphore dont do his job, and there's scenarios where the client sends few messages in a row, and not waiting for reply from the server . can you help me find the bug in my code?
原文:https://stackoverflow.com/questions/37332687
最满意答案
使用
cte
并指定行号,以便删除除重复对之外的所有行。with rownums as (select m.*, row_number() over(partition by ToUserId, FromUserId order by ToUserId, FromUserId) as rnum from Message m) delete r from rownums r where rnum > 1
Use a
cte
and assign row numbers so that all but one for duplicate pairs can be deleted.with rownums as (select m.*, row_number() over(partition by ToUserId, FromUserId order by ToUserId, FromUserId) as rnum from Message m) delete r from rownums r where rnum > 1
相关问答
更多-
你真的很亲密。 使用这些列作为设置条目 entries = set() for row in reader: key = (row[0], row[1]) # instead of just the last name if key not in entries: writer.writerow(row) entries.add(key) You're really close. Use those columns as the set entry entries ...
-
Oracle如何根据多个数据库列和条件删除重复项(Oracle How delete duplicates based on multiples database columns and conditions)[2022-06-13]
我认为你可以使用rowid和相关的子查询来做到这一点: delete from payinfo_staging_db where rowid <> (select rowid from (select rowid from payinfo_staging_db t2 where t2.debitNum = payinfo_staging_db.debitNum an ... -
使用cte并指定行号,以便删除除重复对之外的所有行。 with rownums as (select m.*, row_number() over(partition by ToUserId, FromUserId order by ToUserId, FromUserId) as rnum from Message m) delete r from rownums r where rnum > 1 Use a cte and assign row numbers so that all but ...
-
这会将您的语句组合到两个不同的列比较中,并且只有两个列都匹配时才会找到报告: =IF(AND(IF(ISNA(VLOOKUP(Sheet1!A2,Sheet2!$A$2:$A$100,1,FALSE)),FALSE,TRUE),IF(ISNA(VLOOKUP(Sheet1!B2,Sheet2!$B$2:$B$100,1,FALSE)),FALSE,TRUE)),"Found","Not Found") This combines your statement into two different colum ...
-
此代码将删除工作簿中每个列的重复项 - 将每个列作为单独的实体处理。 Sub RemoveDups() Dim wrkSht As Worksheet Dim lLastCol As Long Dim lLastRow As Long Dim i As Long 'Work through each sheet in the workbook. For Each wrkSht In ThisWorkbook.Worksheets 'Fin ...
-
尝试执行三个单独查询的UNION ,检查三个重复的条件: SELECT id FROM users GROUP BY id HAVING COUNT(DISTINCT email) > 1 UNION ( SELECT id FROM users t1 INNER JOIN ( SELECT firstname, lastname FROM users GROUP BY firstname, lastname H ...
-
查询数据库以查找基于多列的潜在重复项(Querying database to find potential duplicates based on multiple columns)[2023-05-22]
你会使用窗口函数: select t from (select t.*, count(*) over (partition by left(fname, 2), left(lname, 2), dob, sysemid) as cnt from t ) t where cnt > 1 order by sysemid, dob, fname, lname; 这也可能是soundex()实际上有用的罕见情况之一。 毕竟,它的设计考虑了字符串。 所以: sele ... -
使用data.table v1.9.5 - 此处的安装说明: require(data.table) # v1.9.5+ df[!duplicated(rleidv(df, cols = c("X", "Y"))), ] rleidv()最好用例子来理解: rleidv(c(1,1,1,2,2,3,1,1)) # [1] 1 1 1 2 2 3 4 4 为每个连续的值运行生成唯一索引。 同样可以在一组特定列上的list()或data.frame()或data.table()上完成。 例如: df = d ...
-
基于特定列删除CSV文件中的重复项(Deleting Duplicates From CSV File Wtih Multi-Columns Based On A Specific Column)[2023-03-23]
您可以通过在自己的数组中跟踪$phone或每次搜索整个$rows数组来避免在构建数组的同一循环中出现重复的列值。 前一种选择将更有效。 例 $rows = $phones = []; if (($handle = fopen($file_tmp, "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { list($phone, $name, $amount) = $data; ... -
更改创建表的查询以便使用 t1.FN < t2.FN 代替 t1.FN != t2.FN 这将阻止它两次返回相关的行。 Change the query that creates the table so that is uses t1.FN < t2.FN instead of t1.FN != t2.FN This will prevent it from returning the related rows twice.