Announcement Announcement Module
No announcement yet.
TaskExecutor and stopping thread Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • TaskExecutor and stopping thread


    What is the right way to stop thread which was runned by TaskExecutor?

    I can write without it:

    Thread readerThread = new Thread(new Runnable(){
    public void run() {
    while(readerThread == Thread.currentThread()) {

    So, I need only set readerThread = null to stop thread. How can I do it with TaskExecutor?

  • #2
    Originally posted by ENP

    What is the right way to stop thread which was runned by TaskExecutor?
    The TaskExecutor is not meant for executing tasks repeatedly, it is meant to execute a 'short' job and after the job is executed, the threads in the TaskExecutor wait for the next one. If you want to execute a task repeatedly , you need a different threading construct. I have created the 'Repeater' that does that Job, but I haven't released it. You could create something yourself. A Repeater(ThreadPool) has a pool of threads that keeps executing a single task over and over again, untill a new task is set or untill the the task is set to null.

    And you really should watch out with integrating thread creation/destruction stuff within your components. Components will be more complex, difficult to customize, difficult to reuse, hard to test. It is better to externalize the threading.


    class YourRunnable implements Runnable{
       void run(){
           ... do you thing, but don't loop.
    Repeater repeater = new ThreadPoolRepeater(10);//we want 10 threads
    repeater.execute(new YourRunnable());
    In this case the Thread creation/destruction is completely removed from the YourRunnable. You could even ignore the Runnable completely and use some kind of 'MethodInvokingRunnable'.

    <!-- de parser is verantwoordelijk voor het parsen van pagina`s-->
    	<bean id="parser"
    		<!-- hier worden fe-->
    		<constructor-arg index="0" ref="fetcherToParserChannel"/>
    		<!-- hier worden alle succesvolle geparste pagina`s naar toe gestuurd-->
    		<constructor-arg index="1" ref="parserOutputChannel"/>
    		<!-- hier worden alle pagina`s naar toe gestuurd waarbij er problemen zijn opgetreden tijdens het
    		<constructor-arg index="2" ref="deadLetterOutputChannel"/>
    	<bean id="parserRepeater"
    		<!-- number of threads -->
    		<constructor-arg index="0" value="1"/>
    		<constructor-arg index="1">
    			<bean class="org.jph.concurrent.StdThreadFactory">
    				<!-- priority -->
    				<constructor-arg index="0" value="3"/>
    				<!-- De naam van ThreadGroup waar de Threads onder vallen -->
    				<constructor-arg index="1" value="parserRepeaters"/>
    		<constructor-arg index="2">
    			<bean class="org.jph.spring.scheduling.MethodInvokeRunnable">
    					<bean class="org.springframework.util.MethodInvoker">
    						<property name="targetObject" ref="parser"/>
    						<property name="targetMethod" value="processSingleMsg"/>
    And you should watch out with the 'currentThread' field. If that field isn't volatile and is not synchronized, it can happen the loop doesn't see a new value (and the loop won't stop).
    Last edited by Alarmnummer; Jun 22nd, 2006, 08:43 AM.