0

I have switch case in a while loop. Whereas one case has a synchronized blocked in it followed by break statement. This break statement is causing the loop to break instead of case. Below is the code.

while (!shutdown && !Thread.currentThread().isInterrupted()) {
            try {
                logger.info("Trying to Read message");
                ServerResponseMessage response = gson.fromJson(jsonReader, ServerResponseMessage.class);
                logger.info("Message Reading completed.");
                if(response == null) {
                    logger.info("Received EOS Response, closing thread.");
                    break;
                }
                switch (response.getRequestType()) {
                /* GET request type is considered as the request for the new object */
                case GET:
                    object = response.getObject();
                    synchronized (requestLock) {
                        requestLock.notifyAll();
                    }
                    break;
                    /*
                     * If client receives HEARTBEAT message from Server, client need to response as
                     * Ack.
                     */
                case HEARTBEAT:
                    logger.info("Received Hearbeat");
                    ServerResponseMessage message = new ServerResponseMessage(RequestType.HEARTBEAT_ACK, null);
                    JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(serverSocket.getOutputStream()));
                    jsonWriter.jsonValue(new Gson().toJson(message));
                    jsonWriter.flush();
                    logger.info("Sent Hearbeat Ack");
                    break;
                default:
                    break;
                }
                logger.info("Completed reading.");
            } catch (IOException e) {
                logger.warn("Error while reading client", e);
                shutdown = true;
            }
        }

In the above code the GET case causes loop break, as per log it directly jumps out of while loop after notifyAll method. Please help.Thanks in advance.

  • Try adding a label for switch. Just add `MY_LABEL:` above switch and use `break MY_LABEL` instead of `break`. Please let me know the result. – Mahdi-Malv Oct 02 '18 at 06:16
  • 1
    How exactly did you determine which particular `break` statement causes the loop to exit? What makes you sure this is not the `while` condition or `null` response branch? Can you add a few sentences explaining this? – Kostiantyn Oct 02 '18 at 06:17
  • Why not use if-elseif-else conditions instead of switch? – Balwinder Singh Oct 02 '18 at 06:17
  • @Kostiantyn I have observed the log and it falls out of loop when break after notifyAll is completed. – ROHIT IHARE Oct 02 '18 at 06:23
  • Instead of `break` to `MY_LABEL` you can use `continue`. As the keyword says, it will skip the current loop-iteration and [continue](https://stackoverflow.com/questions/389741/what-is-the-continue-keyword-and-how-does-it-work-in-java) with the next loop-iteration. – LuCio Oct 02 '18 at 06:58
  • @Kostiantyn you were right, actually the loop was not breaking due to break statement, but the unhandled socket exception was the culprit. I was closing the socket before re reading it, which was causing socket exception. – ROHIT IHARE Oct 02 '18 at 07:39

1 Answers1

1

Consider a label for your Switch so you can easily break that label when needed.

LOOP1:
while (condition) {
   MY_LABEL:
   switch (variable) {
      case (a):
        break LOOP1; // will break the loop -> Also continue; works as well.
        break MY_LABEL; // it will break the switch and continue on loop
   }
}

This is generally usefull for nested loops.See the docs for more information.

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117