while(true)循环没有中断

时间:2016-07-29 09:14:55

标签: java android while-loop infinite-loop

我是Java编程的新手。目前我正在研究一个名为Evercam的Android应用程序的源代码。但是,我在理解涉及while(true)循环的代码的一部分时遇到了问题。

以下是代码片段:

while (true)
{
    while (true)
    {
        byte[] responseMessageByteArray = new byte[4000];
        DatagramPacket datagramPacketRecieve = new DatagramPacket(responseMessageByteArray, responseMessageByteArray.length);
        datagramSocket.receive(datagramPacketRecieve);
        String responseMessage = new String(datagramPacketRecieve.getData());
        EvercamDiscover.printLogMessage("\nResponse Message:\n" + responseMessage);
        StringReader stringReader = new StringReader(responseMessage);
        InputNode localInputNode = NodeBuilder.read(stringReader);
        EnvelopeProbeMatches localEnvelopeProbeMatches = (EnvelopeProbeMatches)(new Persister()).read(EnvelopeProbeMatches.class, localInputNode);
        if (localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches.size() > 0)
        {
            ProbeMatch localProbeMatch = (ProbeMatch) localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches.get(0);
            if (uuidArrayList.contains(localProbeMatch.EndpointReference.Address))
            {
                EvercamDiscover.printLogMessage("ONVIFDiscovery: Address " + localProbeMatch.EndpointReference.Address + " already added");
            }
            else
            {
                uuidArrayList.add(localProbeMatch.EndpointReference.Address);
                DiscoveredCamera discoveredCamera = getCameraFromProbeMatch(localProbeMatch);
                if (discoveredCamera.hasValidIpv4Address())
                {
                    this.onActiveOnvifDevice(discoveredCamera);
                    cameraList.add(discoveredCamera);
                }
            }
        }
    }
}

这不会产生无限循环吗?我在Java中的基础知识并不强,所以如果有人可以告诉我while(true){//codes}实际上在没有任何中断的情况下实际退出,或者 ,我会非常感激/ strong>退出??

<小时/> 的修改

我直接从android项目文件中实际从反编译中提取此代码段是不好的。我不知道它会有所不同,然后我知道的很少。这是原始的code

    public ArrayList<DiscoveredCamera> probe() {
ArrayList<DiscoveredCamera> cameraList = new ArrayList<DiscoveredCamera>();

try {
    DatagramSocket datagramSocket = new DatagramSocket();
    datagramSocket.setSoTimeout(SOCKET_TIMEOUT);
    InetAddress multicastAddress = InetAddress.getByName(PROBE_IP);

    if (multicastAddress == null) {
    // System.out.println("InetAddress.getByName() for multicast returns null");
    return cameraList;
    }

    // Send the UDP probe message
    String soapMessage = getProbeSoapMessage();
    // System.out.println(soapMessage);
    byte[] soapMessageByteArray = soapMessage.getBytes();
    DatagramPacket datagramPacketSend = new DatagramPacket(
        soapMessageByteArray, soapMessageByteArray.length,
        multicastAddress, PROBE_PORT);
    datagramSocket.send(datagramPacketSend);

    ArrayList<String> uuidArrayList = new ArrayList<String>();
    while (true) {
    // System.out.println("Receiving...");
    byte[] responseMessageByteArray = new byte[4000];
    DatagramPacket datagramPacketRecieve = new DatagramPacket(
        responseMessageByteArray,
        responseMessageByteArray.length);
    datagramSocket.receive(datagramPacketRecieve);

    String responseMessage = new String(
        datagramPacketRecieve.getData());

    EvercamDiscover.printLogMessage("\nResponse Message:\n"
        + responseMessage);

    StringReader stringReader = new StringReader(responseMessage);
    InputNode localInputNode = NodeBuilder.read(stringReader);
    EnvelopeProbeMatches localEnvelopeProbeMatches = new Persister()
        .read(EnvelopeProbeMatches.class, localInputNode);
    if (localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches
        .size() <= 0) {
        continue;
    }

    ProbeMatch localProbeMatch = localEnvelopeProbeMatches.BodyProbeMatches.ProbeMatches.listProbeMatches
        .get(0);
    // EvercamDiscover.printLogMessage("Probe matches with UUID:\n"
    // +
    // localProbeMatch.EndpointReference.Address + " URL: " +
    // localProbeMatch.XAddrs);
    if (uuidArrayList
        .contains(localProbeMatch.EndpointReference.Address)) {
        EvercamDiscover.printLogMessage("ONVIFDiscovery: Address "
            + localProbeMatch.EndpointReference.Address
            + " already added");
        continue;
    }
    uuidArrayList.add(localProbeMatch.EndpointReference.Address);
    DiscoveredCamera discoveredCamera = getCameraFromProbeMatch(localProbeMatch);

    if (discoveredCamera.hasValidIpv4Address()) {
        onActiveOnvifDevice(discoveredCamera);
        cameraList.add(discoveredCamera);
    }
    }
} catch (Exception e) {
    // ONVIF timeout. Don't print anything.
}

原来在实际代码中有continue语句。非常感谢你的回复,我会记得不应该依赖于解编译的类。

3 个答案:

答案 0 :(得分:4)

这个看起来像一个无限循环。为了绝对确定,您必须静态读取每个语句并遵循调用的方法,以查看是否存在任何可能的调用,如Activity#finish()Service#stopSelf(),这些调用将完成当前正在运行的活动,从而有效地打破循环。 / p>

另一种可能性是代码旨在作为后台线程服务在无限循环中运行,而某些其他组件可以选择在达到某个条件时终止该服务。例如,它可以是Runnable类的一部分并在线程池中执行,并且当存在超时时,池将关闭。

答案 1 :(得分:0)

退出while循环的唯一可能方法是,如果循环中调用的方法之一抛出异常。检查这些方法的代码是否有异常或在此处分享

答案 2 :(得分:0)

没有任何中断的无限循环对于在后台运行的服务非常有用。

由于一段时间(真实),您创建了一个无限地执行服务的新线程,当您停止应用程序时,您只需终止与该服务相对应的进程。