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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:29:27+00:00 2026-06-02T22:29:27+00:00

Sorry if this is basic, I just don’t see the answer. Basically I am

  • 0

Sorry if this is basic, I just don’t see the answer. Basically I am running a few threads using ThreadPoolExecutor to launch 10 threads. I want each thread to make its own connection to a server for the duration of its life. Is it possible and if so where do I put this code?

Example code:

class DoWork implements Runnable{

    protected String[] data = null; 

    public DoWork(String[] data) {
        // get the data each thread needs to work
        this.data = data;
    }

    public void run() {
        // Do the work here

    }

}

My understanding has been if there are items in the work queue then ThreadPoolExecutor keeps the 10 threads alive and when the work is done they die. Is there somewhere in this structure I can add connection information? I don’t want to do it in the DoWork method because that’ll be done for each work unit(thus opening connections for as many items in the work queue, could be thousands and causes timeouts when I tried). Putting in-between the class declaration and the method didn’t seem to do anything(although I could be doing it wrong).

Any ideas of how to accomplish this?

UPDATE: not 100% needed but I’m curious if there’s also a way to get it do something upon termination(maybe so I can close the connection to the server instead of waiting for it to timeout).

  • 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-02T22:29:28+00:00Added an answer on June 2, 2026 at 10:29 pm

    You could use a ThreadLocal to store the connection for each thread:

    public class ConnectionProvider {
        private static final ThreadLocal<Connection> threadLocalConnection = 
             new ThreadLocal<Connection>() {
                 @Override 
                 protected Connection initialValue() {
                     // TODO create and return the connection
                 }
             };
    
        public static Connection getConnection() {
            return threadLocalConnection.get();
        }
    }
    

    Or, if the connections can be used (at different times) by several threads, you could create a pool of connections. The run method would get a connection from the pool (which would dynamically create it and add it to the set of created connections is none is available), and release it to the pool (in a finally block) when it has finished using it. This would make the connection available for any other task. This would have the advantage of making the number of needed connections potentially lower than the number of threads in the thread pool.

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

Sidebar

Related Questions

Sorry if the answer to this problem is fairly obvious. I just don't understand
Sorry this is a basic question, but all my research just barely missed answering
Sorry, this might be a basic/stupid/noob question - I am just trying to tweak
I'm pretty new to Java so sorry if this is super basic. Basically I
Sorry if this is really basic but I am just learning PHP. I have
Sorry this is probably super basic. But in all my javabean examples, I've not
Sorry if this is a really basic question but it's been really getting to
I'm sorry if this is a very basic but I couldn't find anything in
This is going to sound too silly / too basic - sorry about that,
Sorry, another super basic ASP.NET question. this so embarrassing. I am reading the article

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.