1

this is my piece of code:

// TODO Auto-generated method stub
        try
        {
            Socket clientSocket = new Socket("localhost", 8888);

            ObjectOutputStream ous = new ObjectOutputStream(clientSocket.getOutputStream());

            while(sending)
            {
                Statistics statsData = setStatisticsData();
                ous.writeObject(statsData);

                Thread.sleep(5000);
            }           
        }
        catch(UnknownHostException uhe) 
        {
            uhe.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        } 
        catch (InterruptedException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

it's the client.it'll sending in an infinite loop an object Statistics. I would implement a way to break this infinite loop through input (es. press a button in the keyboard).ok can I do?how can i break an infinite loop?

user1508419
  • 333
  • 1
  • 4
  • 14

3 Answers3

4

If this is a separate thread:

Thread statisticsSendingThread = ...

Then simply interrupt it:

statisticsSendingThread.interrupt();

InterruptedException will be thrown, escaping from the loop.

I see you already have a boolean sending flag. It will work as well, but with 5 second delay in worst case. Also make sure it is volatile - or better, use isInterrupted() method of thread. But interrupting the thread using interrupt() is by far the easiest way (no extra coding required). And your loop can be truly infinite (while(true) although with while(!isInterrupted())).


BTW your setStatisticsData() should probably be named getStatisticsData().

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Another advantage of using `interrupt()` is that it will interrupt `sleep`, `wait` and various other higher-level blocking calls. You can't achieve that neatly with a custom "have we been killed" flag – Stephen C Jul 11 '12 at 13:21
  • @Tomasz Nurkiewicz but Where I should interrupted the thread? – user1508419 Jul 11 '12 at 13:21
  • @user1508419 - In whatever thread reads the keystroke event or character from the keyboard. – Stephen C Jul 11 '12 at 13:23
  • You should still use .isInterrupted() as your loop condition. If you're in the middle of the writeObject() method, the thread interrupt will not do anything except set the interrupted flag on the current thread. This is true in general for all blocking io (anything in java.io package). Only certain methods like Thread.sleep() will actually respond to an interrupt and throw an exception. – Matt Jul 11 '12 at 17:12
0

You'll have to do them on separate threads, and since you are looking for user input it would probably be best to move your code to some asynchronous thread, then simply on input set your sending bool to false or interrupt the thread YourThread.interrupt();

NominSim
  • 8,447
  • 3
  • 28
  • 38
-1
while(sending) 
{ 
    Statistics statsData = setStatisticsData(); 
    ous.writeObject(statsData); 
    Thread.sleep(5000);
    if(EXIT_FLAG == true)
       sending = false;
}    

EXIT_FLAG can be a static variable that can be updated from outside. You may also want to run this infinite loop in a separate Thread. Read more about how to share data between Threads.

Sree
  • 746
  • 6
  • 21
  • `if(EXIT_FLAG) break;`? And why do you name variable using capital case? Not my downvote though. – Tomasz Nurkiewicz Jul 11 '12 at 13:13
  • "You may also want to run this infinite loop in a separate Thread." How is EXIT_FLAG going to be set if not on separate thread? – NominSim Jul 11 '12 at 13:14
  • @TomaszNurkiewicz - For better readability. Why Calendar.DAY_OF_YEAR is caps? I don't know why the down-vote. But interrupting a Thread is not the solution for this. There has a to be a clean way to come out of the loop. – Sree Jul 11 '12 at 13:17
  • @NominSim - "You may also want to" implicitly says "you would want to". It's just a way of putting it. – Sree Jul 11 '12 at 13:19
  • Not to be picky, but "you may also want to" implies that you either can or can't, but to get the result the OP is looking for it is actually "you have to", there is no choice if he wants results. – NominSim Jul 11 '12 at 13:21