How to delete a stream in Java

Here's the important part of my Server class:

        @SuppressWarnings("resource")
        ServerSocket server = new ServerSocket(8000);

        while (true)
        {                                               
            Socket s = server.accept();             
            System.out.println("Client connected from " + s.getLocalAddress().getHostName());   

            Client chat = new Client(s);
            Thread t = new Thread(chat);
            t.start();
        }

      

As you can see, it creates a new instance of my Client-Class, which is a thread, every time a new connection is established. Here's the important part of my client class:

        @SuppressWarnings("resource")
        Scanner in = new Scanner(socket.getInputStream());
        PrintWriter out = new PrintWriter(socket.getOutputStream());
        String newLine = null;

        while (true)
        {
            try{
                newLine = in.nextLine();
            }
            catch(NoSuchElementException io){
                //Delete this instance
            }
            if(clientName==null){
                clientName = newLine;
            }
            else{
                out.println(clientName+":  "+newLine);
                out.flush();
            }
        }

      

I am currently catching a client break with a try / catch that reacts to the nextLine missing from my socket input stream. As soon as the client closes its chat client, it will go to the catch clause each time, assuming there is no more input stream.

However, due to performance issues, I would now like to delete this Client instance given that it is no longer in use. However, I really don't know how to do it.

I had one idea: Make my client class observable and listen to it in my Server class. As soon as I enter the catch clause, I will notify my observer, which is my Server class in this case, and tell it to set the link on that client instance to zero, effectively removing any references and leaving them garbage collected.

However, I feel like this is not the way to go, but to be honest I can't think of any other option right now.

Any advice?

+3


source to share


2 answers


It looks like your class Client

either extends from Thread

or implements Runnable

. Either way, your class Client

starts an infinite loop while(true)

. If this loop does not exit, the class Client

will not be garbage collected. Therefore, you need to exit the loop while(true)

.

while (true) {
    try{
       newLine = in.nextLine();
    }
    catch(NoSuchElementException io){
        break;// exit the infinite loop
    }
    if(clientName==null) {
       clientName = newLine;
    }
    else {
       out.println(clientName+":  "+newLine);
       out.flush();
    }
}

      



As soon as you exit the loop while(true)

, the thread's execution process ends Client

and the object Client

is eligible for garbage collection. You don't need to explicitly delete the object Client

as this is done by the garbage collector when the object is found in order to be eligible for garbage collection. (Assuming the loop while(true)

is inside your method run

or inside the method that is calling run

)

+3


source


Unless you save the link to Client

anywhere (as it seems in your code), you don't need to do anything (other than close resources).



When your thread exits (for example, by returning / hacking from your catch block), your object Chat

will be eligible for garbage collection.

+3


source







All Articles