Tuesday 27 December 2016

Java Tutorial: Java Threads (Cached Thread pool in java | Java cached thread pool)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=wOqI-EeUWVE&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Java Threads (Cached Thread pool in java | Java cached thread pool) 
Java Tutorial: Java Threads (Cached Thread pool in java | Java cached thread pool) 
ThreadPoolTest.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class ThreadPoolTest
{
    public static void main(String[] args)
    {
        int numOfCallableTasks = 4;

        ExecutorService executorService = Executors.newCachedThreadPool();
        CallableTask callableTasks[] = new CallableTask[numOfCallableTasks];
        Future<?> futures[] = new Future[numOfCallableTasks];

        for (int i = 0; i < numOfCallableTasks; i++)
        {
            callableTasks[i] = new CallableTask(i);
            futures[i] = executorService.submit(callableTasks[i]);
        }
        for (int i = 0; i < numOfCallableTasks; i++)
        {
            try
            {
                System.out.println("Ending task : " + futures[i].get());
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}
CallableTask.java
import java.util.concurrent.Callable;

public class CallableTask implements Callable<Integer>
{
    private int taskNumber;

    CallableTask(int taskNumber)
    {
        this.taskNumber = taskNumber;
    }

    public Integer call()
    {
        for (int i = 0; i <= 100; i += 20)
        {
            // Perform some work ...
            System.out.println(Thread.currentThread().getName()
                    + " taskNumber : " + taskNumber + ", percent complete: "
                    + i);
            try
            {
                Thread.sleep((int) (Math.random() * 1000));
            }
            catch (InterruptedException e)
            {
            }
        }
        return (taskNumber);
    }
}
Output
pool-1-thread-2 taskNumber : 1, percent complete: 0
pool-1-thread-1 taskNumber : 0, percent complete: 0
pool-1-thread-3 taskNumber : 2, percent complete: 0
pool-1-thread-4 taskNumber : 3, percent complete: 0
pool-1-thread-2 taskNumber : 1, percent complete: 20
pool-1-thread-4 taskNumber : 3, percent complete: 20
pool-1-thread-1 taskNumber : 0, percent complete: 20
pool-1-thread-3 taskNumber : 2, percent complete: 20
pool-1-thread-4 taskNumber : 3, percent complete: 40
pool-1-thread-3 taskNumber : 2, percent complete: 40
pool-1-thread-2 taskNumber : 1, percent complete: 40
pool-1-thread-4 taskNumber : 3, percent complete: 60
pool-1-thread-3 taskNumber : 2, percent complete: 60
pool-1-thread-2 taskNumber : 1, percent complete: 60
pool-1-thread-1 taskNumber : 0, percent complete: 40
pool-1-thread-2 taskNumber : 1, percent complete: 80
pool-1-thread-3 taskNumber : 2, percent complete: 80
pool-1-thread-2 taskNumber : 1, percent complete: 100
pool-1-thread-4 taskNumber : 3, percent complete: 80
pool-1-thread-1 taskNumber : 0, percent complete: 60
pool-1-thread-4 taskNumber : 3, percent complete: 100
pool-1-thread-3 taskNumber : 2, percent complete: 100
pool-1-thread-1 taskNumber : 0, percent complete: 80
pool-1-thread-1 taskNumber : 0, percent complete: 100
Ending task : 0
Ending task : 1
Ending task : 2
Ending task : 3
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ThreadDemo_CachedThreadPool_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ThreadDemo_CachedThreadPool_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/0fa116261fbf2fe2bc94946e6ef3f543aabbf896/BasicJava/ThreadDemo_CachedThreadPool_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment