0

Basically I would like to do some multithreading in my spring java program and I don't know how to use my servers spring resources to create a new thread.

I looked at Any good Spring threading with a TaskExecutor examples? but I couldn't figure out where the original TaskExecutor came from, like how would I pass that into the class?

Also is TaskExecutor the best thing to use for just creating another thread in my program?

Community
  • 1
  • 1
Grammin
  • 11,808
  • 22
  • 80
  • 138
  • You mention Spring and Server...any more details on what sort of app this is? Is it a web-app? A desktop app? Some other sort of web service app? A generic processing app that doesn't really have much of a UI (along the lines of a command-line app)? – cdeszaq Mar 13 '12 at 14:33

2 Answers2

0

The TaskExecutor is defined in the xml spring beans definition and passed as constructor's argument to the utility class.

 <constructor-arg type="org.springframework.core.task.TaskExecutor" ref="taskExecutor" />

If you have the choice, you should definitely work with TaskExecutor and Threadpool instead of managing your thread the old way.

Arnaud Gourlay
  • 4,646
  • 1
  • 29
  • 35
  • But how do I actually invoke the new class that I have when I don't have a TaskExecutor to pass into it? – Grammin Mar 13 '12 at 16:30
  • both TaskExecutor and somethingThatShouldHappenInAThread are created by Spring, you just have to get a ref (by injection into your service layer for example) on somethingThatShouldHappenInAThread and do classWithMethodToFire.run(). – Arnaud Gourlay Mar 13 '12 at 17:09
0

The post that you mention gives a good example of what you want to do. taskExecutor is injected via the constructor. Checkout the spring beans configuration given in that post. I believe it is a good practice to use TaskExecutor.

Samarth Bhargava
  • 4,196
  • 3
  • 17
  • 16