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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:12:16+00:00 2026-05-27T08:12:16+00:00

private final ExecutorService executorParsers = Executors.newFixedThreadPool(10); public void parse(List<MyObjInt> objs) { //… bunch of

  • 0
private final ExecutorService executorParsers = Executors.newFixedThreadPool(10);

public void parse(List<MyObjInt> objs) {
   //... bunch of elided stuff ....

   CompletionService<AsupParseObj> parserService = new ExecutorCompletionService<AsupParseObj>(executorParsers);

   for (final AsupStoreObj obj : objs) {
      parserService.submit(new ParseThread(obj));
   }
}

I would like to DI the “ParseThread” but surely there has to be a better way to do this than burying a call to getBean on a prototype scoped bean and as I am new to Spring I figured I would ask…

  • 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-27T08:12:17+00:00Added an answer on May 27, 2026 at 8:12 am

    Here is the full configuration using lookup-method (see 3.4.6.1 Lookup method injection):

    <bean id="executorParsers" class="java.util.concurrent.Executors" 
            factory-method="newFixedThreadPool" 
            destroy-method="shutdownNow">
        <constructor-arg value="10"/>
    </bean>
    
    <bean id="parserService" class="java.util.concurrent.CompletionService">
        <constructor-arg ref="executorParsers"/>
    </bean>
    
    <bean id="foo" class="Foo">
        <lookup-method name="createThread" bean="parseThread"/>
    </bean>
    
    <bean id="parseThread" class="ParseThread" scope="prototype" lazy-init="true"/>
    

    And the Java code:

    abstract class Foo {
    
        @Autowired
        CompletionService parserService;
    
        protected abstract ParseThread createThread();
    
        public void parse(List<MyObjInt> objs) {
            for (final AsupStoreObj obj : objs) {
                ParseThread t = createThread();
                t.setObject(obj);
                parserService.submit(t);
            }
        }
    }
    

    Unfortunately you cannot pass any parameters to lookup-method (see SPR-7431 and my article Creating prototype Spring beans on demand using lookup-method), hence the need for artificial setObject().

    If you don’t like abstract methods/classes, lookup method can be non-abstract no-op method or (better) the default implementation can throw an exception. Spring will override the implementation at runtime, effectively calling getBean() for you.

    Bonus: I translated Executor/CompletionService to Spring managed beans as well. Note that Spring supports these out-of-the-box: Task Execution and Scheduling.

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

Sidebar

Related Questions

First class: class Main1 { private ExecutorService service = Executors.newFixedThreadPool(4); public static void main(String[]
I've got a class somewhat like this: public class Test { private final List<ISomeType>
I came across code to stop execution's task. private final ExecutorService executor = Executors.newSingleThreadExecutor();
public class MainClass { private static final int size = 5; private ExecutorService prodExec
private class DownloadLargeFileTask extends AsyncTask<Void, Void, Void> { private final ProgressDialog dialog; public DownloadLargeFileTask(ProgressDialog
import java.math.BigInteger; import java.util.concurrent.*; public class MultiThreadedFib { private ExecutorService executorService; public MultiThreadedFib(final int
I currently have code that does the following: private final static ExecutorService pool =
Could you please explain why this code is not syntactically correct? private void addEditor(final
public class User { private final String _first_name; private final String _last_name; private final
Is the below class immutable: final class MyClass { private final int[] array; public

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.