0

My task is to make the multiple threads execute the never-ending task/ make a never-ending threads which will run the same 'void' method again and again, until the trigger will finish it.

Here is 1st attempt Execute a continious task via ThreadPoolExecutor

Now trying with Threads, but still can't get the needed result. Here it is:

boolean IsRunning = true;
Integer noThreads = 5;
ThreadGroup tg = new ThreadGroup("A");
Thread[] listThreads = new Thread[noThreads];
tg.enumerate(listThreads);

@RequestMapping(value = "/start_new", method = RequestMethod.POST)
public Callable<String> StartNewTask(@RequestBody LaunchSend sendobj) throws IOException, InterruptedException {

    Runnable runnable = () -> {
        while(IsRunning) {
            MyVoid();
       }
    };

    for (int i = 0; i < noThreads; i++) {
        listThreads[i] = new Thread(runnable);
        listThreads[i].start();
    }

    return () -> "Callable result";
}

 @Async
    void MyVoid(){
       Globals.getInstance().increment();
       System.out.println(Thread.currentThread().getName()+" iteration # "+ Globals.getInstance().Iterator);
    }

The method 'MyVoid' is in while(true) {} cycle, so it's supposed to be executed continually, but it doesn't. Each Thread execute it only once.

Community
  • 1
  • 1
user1935987
  • 3,136
  • 9
  • 56
  • 108
  • So IsRunning is set to false somewhere – Antoniossss Dec 23 '15 at 10:19
  • And what should be your "callable result" ? You should use ExecutorService anyway for that, like you did in mentioned post. Besides, if you want to stop your tasks at some point in time, they are not forever running. – Antoniossss Dec 23 '15 at 10:22
  • So IsRunning is set to false somewhere - NO – user1935987 Dec 23 '15 at 10:26
  • "You should use ExecutorService anyway for that, like you did in mentioned post. Besides, if you want to stop your tasks at some point in time, they are not forever running." if i use Array of threads i also can stop them, at least i need to run them in the way i need first... – user1935987 Dec 23 '15 at 10:27
  • 1) Use executor service 2) IsRunning is set somewhere to false 3) IDK but maybe Spring's default thread implementation kills invoked thread along with completion of request? – Antoniossss Dec 23 '15 at 10:30
  • 1) executor service usage causing the problems described here http://stackoverflow.com/questions/34433348/java-create-an-array-of-continual-threads?noredirect=1#comment56607974_34433348 i just need to done it in any way.. – user1935987 Dec 23 '15 at 10:32
  • Uv pasted wrong link i guess it points to this post. – Antoniossss Dec 23 '15 at 10:36
  • As for what you have mentioned, you are using executor service in wrong way. If you would check documentation for Executor#execute you would notice that: `Executes the given command at some time in the future. The command **may execute in a new thread, in a pooled thread, or in the calling thread**, at the discretion of the Executor implementation.` So you are using it in wrong way, you should `submit` insteed of execute as `execute` can be done on calling thread - causing your request to hang. Just like you have described. – Antoniossss Dec 23 '15 at 10:39
  • The other reason why i think direct usage of 'Thread' will be better is because it's easier to use 'ThreadGroup' then, and i can manage a Thread groups for different users who use this controller by naming a thread group by the name of the user from Spring Security Context. – user1935987 Dec 23 '15 at 11:05
  • @Antoniossss If i use executor.submit(runnable); it submits a task only to one thread, even if i set executor.setCorePoolSize(5); Can you please white an example, in any way, how can i make this working? – user1935987 Dec 23 '15 at 11:27
  • submit will assign task to next idle thread. In your case it is either single threaded pool, or thread gets idle before next submition. For debug purposes use `Executors.newFixedThreadExecutor(10)` and add `Thread.sleep(1000)` in your task - `MyVoid` in your case and you will see. Are you sure that there is no exception in sinde your runnable? It would be swallowed by `ExecutorService`. – Antoniossss Dec 23 '15 at 12:12

0 Answers0