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

  • SEARCH
  • Home
  • 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 8698615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:46:03+00:00 2026-06-13T01:46:03+00:00

In a Thread After calling the start() as you know it will call the

  • 0

In a Thread After calling the start() as you know it will call the run() function in the Runnable class. In the run() function I want the thread stays as long as it receive the “Bye” message from the client. If i put them in the while loop it takes heaps of memory i think and I’m not using the power of thread. By the way i don’t want my thread sleep in the run function by calling Thread.sleep(6000); Is there any other way to stay there in the run function?

  1. If the answer is join where and how and use it? Should I pop it at the beginning of the run function and it stays there until I send the “Bye” from client?

  2. Should I say while((request=in.readLine())!=null){? It did not work because I think it will lose the connection with the client or better say client losing the connection?

  3. Should I say while(Thread.isAlive) and then kill the threat when I receive the “Bye” by calling Thread.stop which is little bit dangerous?

Here is my simplified code:

while(true)
        {   
            ClientWorker w;
            try
            {                   
                w = new ClientWorker(serverSocket.accept());
                    Thread t = new Thread(w);
                    t.start();
                }
......  }

    class ClientWorker implements Runnable {
        public ClientWorker(Socket incoming)
        {
            myList = my;
            this.incoming = incoming;
        }
    public synchronized  void run()
    {
         in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
.
.
..
...
}
  • 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-06-13T01:46:04+00:00Added an answer on June 13, 2026 at 1:46 am

    In the simplest case just let your run method do its work and terminate when appropriate, something like this:

    public void run() {
        boolean running = true;
        while(running) {
            running = doWork();
            if (Thread.interrupted()) {
                return;
            }
        }
    }
    

    Here the process stops when the doWork() method returns false. It is also good style to allow your thread to be interrupted, especially if it needs to run for a long time, see this tutorial. For any of this to work, doWork() should return regularly. Note you cannot restart threads, once the run method has returned the thread is reclaimed by the system.

    If you need more control over your threads you could create separate Worker and ThreadManager classes.

    To let the ThreadManager terminate the Worker, create a volatile boolean field in your Worker which is checked periodically:

    public class Worker extends Thread {
    
        private volatile boolean running;
    
        public void run() {
            running = true;
            while(running) {
                running = doWork();
                if (Thread.interrupted()) {
                    return;
                }
            }
        }
    
        public void stopRunning() {
            running = false;
        }
    }
    

    The Worker ends when interrupted or when the work is completed. Also the ThreadManager can request the Worker to stop by invoking the stopRunning() method.

    If your thread runs out of work it could also call the wait() method on the ThreadManager. This pauses the thread until it is notified that there is new work to do. The ThreadManager should call notify() or notifyAll() when new work arrives (ThreadManager is used as a monitor in this example).

    With this approach you can keep the Worker simple and only concerned with doing the work. The ThreadManager determines the number of threads and makes work available to them but does not need to know details of the actual work. One step further would be to split out ‘work’ into a separate class too, which you could call Job. An example of this can be found in my webcrawler example. This could be useful for passing new work from the ThreadManager to the worker thread.

    EDIT: clarification: If the thread doesn’t do any work and just needs to wait for some condition, don’t use an iteration to wait, but use either wait/notify (see the webcrawler example I gave) or a simple callback when the condition arises.

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

Sidebar

Related Questions

I want to interrupt a thread after a fixed amount of time. Someone else
Background The Apache Action class is not thread-safe. However, this was only realized after
An Unstarted thread is transitioned into the Running state by calling Start. [from msdn
I have service with thread, From activity I am calling start service(from Activity::onDestroy()) and
I want to start 2 background threads. One thread is acting as a server
I wanna kill the TCP connection listener thread(serverside) after client closes the socket.. The
After following the great advice given in a thread about service beans I have
I was confused that a thread is automatically stops after executing the return statement
I have the following thread code which executes correct first time. After that from
Network protocol parser code, parse below layers in single thread. After parsing some procotol

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.