thread from java.util.concurrent

344 views Asked by At

I have code with using thread but i don't use thread from java.util.concurrent so i want change my code, but i have some issue.

Thread thread = new Thread() {
                    public void run() {
                        doSomething();
                    }
                };

I want use executor like this Thread, so how I can do this?Is similar way to do this in java.util.concurrent?I tried that:

ExecutorService executorService = Executors.newCachedThreadPool();
Runnable runnable = new Runnable() {
                    public void run() {
                        //my code doing something
                    }
                };

Also I Have:

List<Runnable> threadsList = new ArrayList<Runnable>();

and I have method:

boolean isAllowedCreateNewThread(Runnable taskToRun){
for(int i = 0; i < threadsList.size(); i++){
        Runnable th = threadsList.get(i);
        if(th.isAlive())doSomething(); //isAlive doesn't work since I have runnable instead of Thread
boolean isAllowed = true ;//I have here some bool function but it doesn't matter
    if(isAllowed){
        threadsList.add(taskToRun);
        //service.submit(taskToRun); 
        executorService.execute(taskToRun);
    }
return isAllowed; 
}

if I have List<Thread> threadsList = new ArrayList<Thread>(); and don't use ExecutorService and change everywhere Runnable to Thread it work's. So I think that it also I have to change, but how? What is Keyword insted Thread?Runnable? Or something else? Also I have:

for(int i = 0; i < threadsList.size(); i++){
        Thread th = threadsList.get(i);
        if(th.isAlive())doSomething(); myThreadName.start();

I have to change isAlive() to something similar in java.util.concurrent and myThreadName.start(); So generally I want change this code using Thread class to code using threads from java.util.concurrent

2

There are 2 answers

0
Sourav Purakayastha On

You can replace your code with below code segment.

  1. Create a Runnable class.

    class MyRunable implements Runnable{
        public void run() {
            doSomething();
        }
        private void doSomething() {
            System.out.println("Thread Executing is " + Thread.currentThread().getId());
        }
    }
    
  2. Create a executor service with required number of threads. This creates a pool of required number of threads.

    int numberOfThreads  = 4;  // number of threads you want to create
    ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);
    
  3. Create your Runnable for each thread present in the pool and assign it to your executor.

    for(int i=0;i<numberOfThreads;i++){
        Runnable myRunable = new MyRunable();
        service.submit(myRunable);
    }
    
    service.shutdown(); // this is a must as not given ,it halts the termination of the program
    

    Sample Output

    Thread Executing is 9
    Thread Executing is 10
    Thread Executing is 8
    Thread Executing is 11
    

So the entire code will look like:

class MyRunable implements Runnable {
    public void run() {
        doSomething();
    }

    private void doSomething() {
        System.out.println("Thread Executing is " + Thread.currentThread().getId());
    }

    public static void main(String[] args) {
        int numberOfThreads  = 4;  // number of threads you want to create
        ExecutorService service = Executors.newFixedThreadPool(numberOfThreads);

        for(int i=0;i<numberOfThreads;i++){
            Runnable myRunable = new MyRunable();
            service.submit(myRunable);
        }

        service.shutdown();

    }

}
0
Michał Szewczyk On

You can use Executors from java.util.concurrent.
Executors are designed to create and manage Threads, so you don't have to do that directly.

In your example it would be:

ExecutorService executorService = Executors.newCachedThreadPool();
executorService.execute(() -> doSomething());


There are many types of Executors, and you have to decide which to use considering the context of invocation.
For instance, if you want to have only one thread, you can use:

Executors.newSingleThreadExecutor();

fixed number of Threads i.e. 10:

Executors.newFixedThreadPool(10);

or create new threads as needed:

Executors.newCachedThreadPool();


For more about Executors you can read here.
Hope it helps.