Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:25:17+00:00 2026-05-31T12:25:17+00:00

This is my run method, it opens a socket, and waiting for an accepted

  • 0

This is my run method, it opens a socket, and waiting for an accepted connection, if connection is accepted, will have a separate Thread open for execute it:

while (isKeepRun) {

    socket = serverSocket.accept();

    WorkTask worktask = new WorkTask();
    worktask.setSocket(socket);
    worktask.setIn(new ObjectInputStream(socket.getInputStream()));
    worktask.setOut(new ObjectOutputStream(socket.getOutputStream()));

    Thread wt = new Thread(worktask);
    wt.start();


}


if (socket != null) {
    socket.close();
}

if (serverSocket != null) {
    serverSocket.close();
}

When the user call it to stop, they call this method, to change the while loop condition. in order to break the while loop:

public void stopWorking() {
    isKeepRun = false;
}

Well, the WorkTask’s run method is very simple like that:

    try {

        do {
            objectOutputStream.flush();
            receivedObj = objectInputStream.readObject();

            if (receivedObj != null){
                System.out.println(receivedObj.toString()+"             " + receivedObj.hashCode());
            }

        } while (receivedObj != null
                && !receivedObj.equals(SharedConstant.SOCKET_EOF_STRING));

        if (objectInputStream != null) {
            objectInputStream.close();
        }
        if (objectOutputStream != null) {
            objectOutputStream.close();
        }

    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

The problem is assume that reading one message need 1 second per message, the user may give up to 100 messages, that’s mean it requires 100 seconds to run, in the socket. When the isKeepRun is keep running, there is no problem. But when the user wanna to stop , and call stopWorking, the loop will be escaped, can the socket is closed, during the WorkTask is reading the message. How can I delay the execution of stopWorking if the socket is still reading, if the socket is finished reading, and the stopWorking will be call immediately, but if the socket don’t have any thing to read, I can call the stopWorking in no delay?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:25:18+00:00Added an answer on May 31, 2026 at 12:25 pm

    There is a problem in your code:

    while (isKeepRun) {

    socket = serverSocket.accept();
    ....
    

    }
    ….

    if (socket != null) {

    socket.close();
    

    }

    You use only one socket reference for all client socket. So if there are more than one client socket, then you will only close the last socket.

    As above said, you can use wt.join() to wait for the worker thread to finish, then the main thread will finish. But you have only one reference for client socket. Even you wait, you can only wait for the last socket to finish. All the previous client socket will be closed if you set it to stop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread with this run method: public void run(){ MAPTable t1 =
When i run this code the paintComponent method is not being called It may
I have this code: try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { try
I have a test method created using Selenium, something similar to this: [TestFixture] public
I am getting this run time error when using IID_IWebBrowser2 interface of CLSID_InternetExplorer. Run-Time
I want to do this: run a command capture the output select a line
I run this piece of code on Visual C++ 2010 char c[10]; cin.get(&c[0],5); cin.get(&c[2],4);
I run this command : select * from LIST where JCODE = 8 and
I run this in localhost and it works. In the server the PHP script
when I run this JS in FF or Safari it works just right but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.