Friday 30 December 2016

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



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

Click the below Image to Enlarge
Java Tutorial: Java Threads (Thread pool executor | Thread pool in java | Java thread pool)
Java Tutorial: Java Threads (Thread pool executor | Thread pool in java | Java thread pool)
Java Tutorial: Java Threads (Thread pool executor | Thread pool in java | Java thread pool)
Java Tutorial: Java Threads (Thread pool executor | Thread pool in java | Java thread pool)
Java Tutorial: Java Threads (Thread pool executor | Thread pool in java | Java thread pool)
RejectedExecutionHandlerImpl.java
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler
{
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor)
    {
        System.out.println(r.toString() + " is rejected");
    }
}
MonitorThread.java
import java.util.concurrent.ThreadPoolExecutor;

public class MonitorThread implements Runnable
{
    private ThreadPoolExecutor executor;

    private int seconds;

    private boolean run = true;

    public MonitorThread(ThreadPoolExecutor executor, int delay)
    {
        this.executor = executor;
        this.seconds = delay;
    }

    public void shutdown()
    {
        this.run = false;
    }

    @Override
    public void run()
    {
        while (run)
        {
            System.out.println(String.format(
                    "[monitor] [%d/%d] Active: %d, Completed: %d, "
                            + "Task: %d, isShutdown: %s, isTerminated: %s",
                    this.executor.getPoolSize(),
                    this.executor.getCorePoolSize(),
                    this.executor.getActiveCount(),
                    this.executor.getCompletedTaskCount(),
                    this.executor.getTaskCount(), this.executor.isShutdown(),
                    this.executor.isTerminated()));
            try
            {
                Thread.sleep(seconds * 1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }

    }
}
RunnableTask.java
public class RunnableTask implements Runnable
{

    private String taskNumber;

    public RunnableTask(String taskNumber)
    {
        this.taskNumber = taskNumber;
    }

    @Override
    public void run()
    {
        System.out.println(Thread.currentThread().getName()
                + " Start. taskNumber = " + taskNumber);
        processCommand();
        System.out.println(Thread.currentThread().getName()
                + " End of taskNumber =" + taskNumber);
    }

    private void processCommand()
    {
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public String toString()
    {
        return this.taskNumber;
    }
}
ThreadPool.java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPool
{

    public static void main(String[] args) throws InterruptedException
    {
        // RejectedExecutionHandler implementation
        RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();
        // Get the ThreadFactory implementation to use
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        // creating the ThreadPoolExecutor
        ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 10,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2),
                threadFactory, rejectionHandler);
        // start the monitoring thread
        MonitorThread monitor = new MonitorThread(executorPool, 3);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();
        // submit work to the thread pool
        for (int i = 1; i <= 10; i++)
        {
            executorPool.execute(new RunnableTask("task" + i));
        }

        Thread.sleep(30000);
        // shut down the pool
        executorPool.shutdown();
        // shut down the monitor thread
        Thread.sleep(5000);
        monitor.shutdown();
    }
}
Output
pool-1-thread-1 Start. taskNumber = task1
pool-1-thread-2 Start. taskNumber = task2
pool-1-thread-3 Start. taskNumber = task5
task7 is rejected
task8 is rejected
pool-1-thread-4 Start. taskNumber = task6
task9 is rejected
task10 is rejected
[monitor] [0/2] Active: 0, Completed: 0, Task: 5, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End of taskNumber =task1
pool-1-thread-1 Start. taskNumber = task3
pool-1-thread-2 End of taskNumber =task2
pool-1-thread-2 Start. taskNumber = task4
pool-1-thread-3 End of taskNumber =task5
pool-1-thread-4 End of taskNumber =task6
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End of taskNumber =task3
pool-1-thread-2 End of taskNumber =task4
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
Refer:
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ThreadDemo_ThreadPoolExecutor_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/d6d97a164a2acde3c2aa603063db8720e3eaea01/BasicJava/ThreadDemo_ThreadPoolExecutor_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