简体   繁体   English

JAVA使用类在线程之间同步操作

[英]JAVA synchronize operations between threads using classes

I am currently trying to create sort of a reliable UDP protocol where my server sends packets of 1024 bytes each to my client, when it finishes a send/receive cycle the server waits and the client sends a command over TCP to the server that tells it what packets were not received (I am using the first 2 bytes as a packet id [of type short]), the server than is notified by a remote class and sends the missing packets. 我目前正在尝试创建一种可靠的UDP协议,其中我的服务器向客户端发送每个1024字节的数据包,当它完成发送/接收周期时,服务器等待,客户端通过TCP向服务器发送一条命令,告诉它没有收到什么数据包(我使用前2个字节作为数据包ID [短类型]),然后由远程类通知服务器并发送丢失的数据包。

private void waitUntilNotified() {
    synchronized (this) {
        try {
            System.out.println("Waiting..." + new Date());
            waiting = true;
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Stop waiting");
    }
}

public void notifyThis(){
    synchronized (this) {
        if(waiting){
            waiting = false;
            notify();
            System.out.println("Notified " + new Date());
            return;
        }
    }
}

My problem is that sometimes on local testing the command arrives before the server starts waiting, therefore causes it to wait forever. 我的问题是,有时在本地测试中,命令会在服务器开始等待之前到达,因此导致其永远等待。

I was wandering if there was a foolproof way of making sure that even if the command that triggers notifyThis() comes before the server triggers the waitUntilNotified() function it will still notify it and change the waiting status? 我在徘徊是否有一种万无一失的方式来确保即使触发notifyThis()的命令在服务器触发waitUntilNotified()函数之前出现,它是否仍会通知它并更改等待状态?

If anything is unclear just comment and I'll try to explain it. 如果不清楚,请发表评论,我会尽力解释。

TL:DR, I am looking for a way to call notify() only after the server starts to wait() even if the call is made before it starts waiting TL:DR,我正在寻找一种仅在服务器开始wait()之后才调用notify()的方法,即使该调用是在它开始等待之前进行的

It looks to me like what you really need is a higher level concurrency primitive. 在我看来,您真正需要的是更高级别的并发原语。 Here are some possibilities: 这里有一些可能性:

or possibly some kind of Queue or Deque . 或者可能是某种QueueDeque


The problem (for us) is that we can't see how you are actually using your wait / notify methods, so it is not clear what you really need. 问题(对我们而言)是我们看不到您实际上如何使用wait / notify方法,因此尚不清楚您真正需要什么。

But as a general principle it is better in the long term to identify and use an appropriate higher-level concurrency class than to try to implement your own concurrency from the basic building blocks. 但是从长远来看, 总的来说 ,识别并使用适当的更高级别的并发类比尝试从基本构件中实现自己的并发要好。

And ... as @DB notes ... implementing your own "reliable" transport over the top of UDP is reinventing the wheel. 而且,正如@DB所指出的那样……在UDP之上实现自己的“可靠”传输正在彻底改变这种状况。 TCP is the obvious alternative, and if you are doing this to get "better than TCP" performance, there are other alternatives. TCP是明显的替代方案,如果您这样做是为了获得“优于TCP”的性能,则还有其他替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM