简体   繁体   English

仅在调试模式下工作

[英]Only works in debugging mode

I'm trying to create a program for a distributed system. 我正在尝试为分布式系统创建程序。 At the moment I have a thread for listening to connections, and a thread for sending, and a thread for receiving. 目前,我有一个用于侦听连接的线程,一个用于发送的线程和一个用于接收的线程。

I've reached a problem where the client will connect but only when using breakpoints. 我遇到了一个问题,即只有在使用断点时,客户端才会连接。 I can't figure out the problem at all!. 我根本解决不了这个问题! I've tried to implement things to slow the program down however nothing is working. 我试图实现一些东西来减慢程序速度,但是什么也没用。

If you guys could take a look i'd be greatly appreciative. 如果你们能看看,我将不胜感激。

  public static void main(String[] args) { System.out.println("Server starting on port 5000"); RecievingConnection reciever = new RecievingConnection(5000,0); //Recieving Connection reciever.start(); SendingConnection sender = new SendingConnection(5001,1); //Sending Connection sender.start(); while(true){ while(reciever.ready==true){ System.out.println("In"); nodes first = new nodes(reciever.socket,0); System.out.println("Node created"); first.start(); System.out.println("Client connected on port: " + reciever.socket.getLocalAddress()); nodes second = new nodes(sender.socket,1); second.start(); reciever.ready=false; sender.ready=false; reciever.connectionComplete=true; sender.connectionComplete=true; } } } 

  public RecievingConnection(int port, int mode) { Serverport = port; connectionMode = mode; try{ server = new ServerSocket(port); server.setSoTimeout(100000); } catch(IOException ex) { System.out.println(ex); } } public void run(){ while(true){ if(ready == false){ try { socket = server.accept(); ready = true; System.out.println("Attempting to connect using port: " + Serverport); while(connectionComplete == false){ //wait } } catch (IOException ex) { System.out.println(ex); } } } } 

The sending thread is basically the same code. 发送线程基本上是相同的代码。 Any idea what the problem is? 知道是什么问题吗? "Nodes" is the thread for each node. “节点”是每个节点的线程。

Your problem is almost certainly at: 您的问题几乎可以肯定在:

    while (connectionComplete == false) {
        //wait
    }

This will loop forever, the other thtreads will not get any cpu time at all. 这将永远循环,其他线程完全不会获得任何CPU时间。 It also explains why it works in debug - it's because in debug if you stop at a breakpoint, any other threads will get time. 它还解释了为什么它可以在调试中工作-这是因为在调试中,如果您在断点处停止,则任何其他线程都会获得时间。

You should at least do: 您至少应该这样做:

    while (connectionComplete == false) {
        //wait
        Thread.sleep(0);
    }

and maybe use a number much greater than 0 . 并可能使用比0大得多的数字。 This will allow other thtreads a chance to run. 这将使其他螺纹有运行的机会。

I am not suggesting that this will make your code work correctly but it should remove the current problem. 我不建议这样做可以使您的代码正常工作,但是应该可以解决当前的问题。


After that there's another tight loop that won't let any other thread time. 之后,还有另一个紧密循环,不会让任何其他线程花费时间。

    while (true) {
        if (ready == false) {

Change that to: 更改为:

    while (true) {
        if (ready == false) {
            // ...
        } else {
            // Here too.
            Thread.sleep(0);
        }

    }

I solved it by adding a sleep in the main thread. 我通过在主线程中添加睡眠来解决它。 I presume this is because the main thread takes priority over child threads? 我想这是因为主线程比子线程具有优先权?

You can't just share variables between threads. 您不能只在线程之间共享变量。 Make them volatile, synchronize or use CAS types like AtomicBoolean. 使它们易失,同步或使用诸如AtomicBoolean之类的CAS类型。

Read about it. 阅读有关它。

https://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html https://docs.oracle.com/javase/tutorial/essential/concurrency/interfere.html

http://www.journaldev.com/1061/java-synchronization-and-thread-safety-tutorial-with-examples http://www.journaldev.com/1061/java-synchronization-and-thread-safety-tutorial-with-examples

https://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html https://docs.oracle.com/javase/tutorial/essential/concurrency/atomicvars.html

Besides class "nodes" is not described here and classes are expected to start with an upper case letter. 除了类“节点”之外,这里没有描述,并且类应该以大写字母开头。

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

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