0

How can I create a new thread in a webservice? I need the thread to start on the "main" of the web service, NOT IN SERVER CALLS.

I need it as worker thread, so server calls will send messages to the thread - for work to be done in BG and will not block the server request.

I have no idea how to do it

2 Answers 2

1

You need to create a ThreadPool, this is an object that you can submit tasks to which will then be executed on the thread(s) of that threadpool.

The easiest way to make a ThreadPool is to use one created by the Executors class.

Sign up to request clarification or add additional context in comments.

1 Comment

This is not clear from the question, but if the author is not creating it's own implementation and uses third-party ws stack, then we have problem here: most implementations already use thread pools and each thread in a pool is continuously reused. I'd consider starting new threads as a bad practice in that case.
1

You could try using a static initialiser in your main class.

Here I am using one to create a timer task which re-reads my properties file every hour but you can use them for almost anything, including making new threads.

static {
  // Read my properties at start-up.
  readProperties ();
  // Start a new timer task to repeat every hour.
  int rate = Debug? 60*1000 : 1*60*60*1000;
  // Make a daemon scheduled thread to re-read properties.
  new Timer("Read properties timer", true).schedule( new TimerTask(){
      public void run() { 
          readProperties();
      }
    }, rate, rate);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.