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 7985583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:35:55+00:00 2026-06-04T11:35:55+00:00

I store threads in a container. With time, some of these threads will be

  • 0

I store threads in a container. With time, some of these threads will be running, and some of these will be dead. What i want to achieve is: automatically (or periodically) remove the dead (stopped) threads from the container.
What is the best way to do this?

Edit: I store my threads in a simple linked-list:

LinkedList<ServerThread> threadPool = new LinkedList<ServerThread>();

This container have to be dynamic, because with time, i have to add (and obviously delete) threads.

EDIT2: This is how i currently manage threads. As you can see, i wait for incoming connections, i don’t know when it will arrive, but when it does, i have to handle it in a new thread.

while (!interrupted()) {
            try {
                Socket clientSocket = serverSocket.accept();
                if (portNumber == Server.SMTP_PORT_NUMBER) {
                    threadPool.add(new SMTPThread(clientSocket, db));
                    threadPool.getLast().setName("SMTP Thread " + ++threadCounter);
                } else {
                    threadPool.add(new POP3Thread(clientSocket, db));
                    threadPool.getLast().setName("POP3 Thread " + ++threadCounter);
                }

                threadPool.get(threadPool.size() - 1).start();

            } catch (SocketTimeoutException e) {
            } catch (IOException ioe) {
            }
        }
  • 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-04T11:35:57+00:00Added an answer on June 4, 2026 at 11:35 am

    You should not maintaining your own list of threads unless there are specific reasons to do so. I’d recommend using the excellent ExcecutorService classes that have been available since Java 5. Something like the following:

    // create a thread pool with 10 workers
    ExecutorService threadPool = Executors.newFixedThreadPool(10);
    // or you can create an open-ended thread pool
    // ExecutorService threadPool = Executors.newCachedThreadPool();
    for (Job job : jobsToDo) {
        threadPool.submit(new MyJobProcessor(job));
    }
    // once we have submitted all jobs to the thread pool, it should be shutdown
    threadPool.shutdown();
    ...
    public class MyJobProcessor implements Runnable {
        private Job job;
        public MyJobProcessor(Job job) {
            this.job = job;
        }
        public void run() {
            // process the job
        }
    }
    

    The thread-pool will take care of maintaining the running threads in the pool. The threads will be re-used on the next job and will be shutdown when there are no more jobs in the pool and it has been shutdown. You won’t need to reap any dead threads yourself.


    Edit:

    In terms of the code you posted, to remove finished threads from your pool you should just run through them like:

     Iterator<ServerThread> iterator = threadPool.iterator();
     while (iterator.hasNext()) {
         ServerThread thread = iterator.next();
         if (!thread.isAlive()) {
            // remove it from the linked list
            iterator.remove();
         }
     }
    

    I’d do this after each time you add a new thread to the pool. Also, remember that LinkedList is not efficient to do a get(#) method call. I’d recommend you tweak your code to do:

                Socket clientSocket = serverSocket.accept();
                ServerThread serverThread;
                if (portNumber == Server.SMTP_PORT_NUMBER) {
                    serverThread = new SMTPThread(clientSocket, db);
                    serverThread.setName("SMTP Thread " + ++threadCounter);
                } else {
                    serverThread = new POP3Thread(clientSocket, db);
                    serverThread.setName("POP3 Thread " + ++threadCounter);
                }
                serverThread.start();
                threadPool.put(serverThread);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I store some dates in my NSMutableArray, I want to retrieve them and change
I have quite a large project to accomplish and I'm running into some dead
I used 5 threads to create new InetSocketAddress and store them in queue, but
I store serialized data in the registry. I want to use a foreach loop
I store objects of a custom data type in QStandardListItems. I recover these objects
I store file’s attributes (size, update time…) in database. So the problem is how
I store user editable articles in a database. Users can insert some simple widgets
I've been working on a Play application (1.2.4) which will do some crunching on
STORE = { item : function() { } }; STORE.item.prototype.add = function() { alert('test
I store various user details in my MySQL database. Originally it was set up

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.