The executor service in java & # 8594; how to convert the code of a single thread for use by an artist

Excuse me if the question sounds silly - I'm just starting to use Executor.

I have an existing Java application that uses threads this way - mostly autonomous threads are used -

private Thread spawnThread( ) { Thread t = new Thread() { String taskSnap = task.toString(); public void run() { try { println( task.run( null ) ); }catch( InterruptedException e ) { println( "ITC - " + taskSnap + " interrupted " ); } } }; return t; } 

As you can see above, the function returns a new thread.

Now in the main () function of the program a new thread is created in this way -

  taskThread = spawnThread(); taskThread.start(); 

What I want to do is create an executor service (with a fixed number of threads) →, and then cancel the creation of a new thread / task execution by a new thread to this executor.

Since I am very new to Executor, I want to know how to modify the above code so that instead of a new separate thread, a new thread is created in the thread pool. I do not see any command to create a thread (in the thread pool) -> pass the above task to this thread (and not to a separate thread, as indicated above).

Please let me know how to solve this problem.

+8
java multithreading executorservice threadpool
source share
3 answers

Basically you can write something like the following:

 ExecutorService executor = Executors.newFixedThreadPool(nThreads); executor.submit(new Runnable() { String taskSnap = task.toString(); public void run() { try { println(task.run(null)); } catch( InterruptedException e) { println("ITC - " + taskSnap + " interrupted "); } } }); 

The submit method will execute Runnable on one of the threads in the executing service.

Note. Remember to close the artist service when you no longer need it, or it will prevent the exit of your program.

+8
source share

Do your research before asking. The idea is to create a class that implements Runnable and executes it using an executing service.

Example from: Java Concurrency (multithreading) - tutorial

Implementation of the worker (which implements Runnable):

 package de.vogella.concurrency.threadpools; /** * MyRunnable will count the sum of the number from 1 to the parameter * countUntil and then write the result to the console. * <p> * MyRunnable is the task which will be performed * * @author Lars Vogel * */ public class MyRunnable implements Runnable { private final long countUntil; MyRunnable(long countUntil) { this.countUntil = countUntil; } @Override public void run() { long sum = 0; for (long i = 1; i < countUntil; i++) { sum += i; } System.out.println(sum); } } 

How to use the executing service to start the start of a workflow.

 package de.vogella.concurrency.threadpools; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Main { private static final int NTHREDS = 10; public static void main(String[] args) { //You can also use Executors.newSingleThreadExecutor() if you just need 1 thread ExecutorService executor = Executors.newFixedThreadPool(NTHREDS); for (int i = 0; i < 500; i++) { Runnable worker = new MyRunnable(10000000L + i); executor.execute(worker); } // This will make the executor accept no new threads // and finish all existing threads in the queue. executor.shutdown(); // Wait until all threads are finish //while (!executor.isTerminated()) { //} //System.out.println("Finished all threads"); //All the threads might not be finished at this point of time. Thread endtime purely depends on the time taken by the processing logic inside your thread. } } 
+5
source share

Do you mean something like this?

 class Parallel { private ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); public void shutdown() { pool.shutdown(); } public void foo() { pool.submit(new Runnable() { @Override public void run() { // put your code here } }); } } 
+1
source share

All Articles