Friday, 30 December 2016

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


Click here to watch in Youtube :
https://www.youtube.com/watch?v=6Y4QjBBicyo&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V5) 
PrintCharTask.java
class PrintCharTask implements Runnable
{
    private char character;
    private int noOfTimes;

    PrintCharTask(char ch, int n)
    {
        character = ch;
        noOfTimes = n;
    }

    public void run()
    {
        for (int i = 0; i < noOfTimes; i++)
        {
            System.out.println(character + " ");
        }
    }
}
Threadpool.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class Threadpool
{
    public static void main(String[] args)
    {
        System.out.println("Main Thread starts");
        ExecutorService threadExecutor = Executors.newFixedThreadPool(2);
        
        PrintCharTask taskl = new PrintCharTask('*', 5);
        PrintCharTask task2 = new PrintCharTask('S', 5);
        PrintCharTask task3 = new PrintCharTask('M', 5);
        PrintCharTask task4 = new PrintCharTask('N', 5);
        
        threadExecutor.execute(taskl);
        threadExecutor.execute(task2);
        threadExecutor.execute(task3);
        threadExecutor.execute(task4);
        /*
         * Tells the threadExecutor to shutdown. No new task
         * can be accepted but the existing task will
         * continue to finish.
         */
        threadExecutor.shutdown();
        /*
         * In order to ensure that the main thread finishes
         * last i.e. all tasks are finished before the main
         * thread terminates, we put the below while
         * statement.
         */
        while (!threadExecutor.isTerminated())
        {
        }
        System.out.println("\nMain Thread Ends");
    }
}
Output
Main Thread starts
* 
* 
* 
* 
* 
M 
M 
M 
M 
M 
N 
N 
N 
N 
N 
S 
S 
S 
S 
S 

Main Thread Ends

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/d6d97a164a2acde3c2aa603063db8720e3eaea01/BasicJava/ThreadDemo_ThreadPool_V5_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
  • Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V4)


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

    Click the below Image to Enlarge
    Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V4) 
    Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V4) 
    Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V4) 
    Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V4) 
    Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V4) 
    SimpleThreadPool.java
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class SimpleThreadPool
    {
    
        public static void main(String[] args)
        {
            ExecutorService executor = Executors.newFixedThreadPool(5);
            for (int i = 0; i < 10; i++)
            {
                Runnable runnableTask = new RunnableTask(" " + i);
                executor.execute(runnableTask);
            }
            executor.shutdown();
            while (!executor.isTerminated())
            {
            }
            System.out.println("Finished all threads");
        }
    }
    
    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;
        }
    }
    
    Output
    pool-1-thread-2 Start. taskNumber =  1
    pool-1-thread-4 Start. taskNumber =  3
    pool-1-thread-3 Start. taskNumber =  2
    pool-1-thread-1 Start. taskNumber =  0
    pool-1-thread-5 Start. taskNumber =  4
    pool-1-thread-2 End of taskNumber = 1
    pool-1-thread-2 Start. taskNumber =  5
    pool-1-thread-4 End of taskNumber = 3
    pool-1-thread-4 Start. taskNumber =  6
    pool-1-thread-1 End of taskNumber = 0
    pool-1-thread-1 Start. taskNumber =  7
    pool-1-thread-5 End of taskNumber = 4
    pool-1-thread-5 Start. taskNumber =  8
    pool-1-thread-3 End of taskNumber = 2
    pool-1-thread-3 Start. taskNumber =  9
    pool-1-thread-4 End of taskNumber = 6
    pool-1-thread-2 End of taskNumber = 5
    pool-1-thread-3 End of taskNumber = 9
    pool-1-thread-1 End of taskNumber = 7
    pool-1-thread-5 End of taskNumber = 8
    Finished all threads
    
    
    Refer:
    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html

    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/d6d97a164a2acde3c2aa603063db8720e3eaea01/BasicJava/ThreadDemo_ThreadPool_V3_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
  • 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
  • Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V3)


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

    Click the below Image to Enlarge
    Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V3)
    Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V3)
    Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V3)
    SimpleThreadPool.java
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    public class SimpleThreadPool
    {
    
        public static void main(String[] args)
        {
            ExecutorService executor = Executors.newFixedThreadPool(5);
            for (int i = 0; i < 10; i++)
            {
                Runnable runnableTask = new RunnableTask(" " + i);
                executor.execute(runnableTask);
            }
            executor.shutdown();
            while (!executor.isTerminated())
            {
            }
            System.out.println("Finished all threads");
        }
    }
    
    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;
        }
    }
    
    Output
    pool-1-thread-2 Start. taskNumber =  1
    pool-1-thread-4 Start. taskNumber =  3
    pool-1-thread-3 Start. taskNumber =  2
    pool-1-thread-1 Start. taskNumber =  0
    pool-1-thread-5 Start. taskNumber =  4
    pool-1-thread-2 End of taskNumber = 1
    pool-1-thread-2 Start. taskNumber =  5
    pool-1-thread-4 End of taskNumber = 3
    pool-1-thread-4 Start. taskNumber =  6
    pool-1-thread-1 End of taskNumber = 0
    pool-1-thread-1 Start. taskNumber =  7
    pool-1-thread-5 End of taskNumber = 4
    pool-1-thread-5 Start. taskNumber =  8
    pool-1-thread-3 End of taskNumber = 2
    pool-1-thread-3 Start. taskNumber =  9
    pool-1-thread-4 End of taskNumber = 6
    pool-1-thread-2 End of taskNumber = 5
    pool-1-thread-3 End of taskNumber = 9
    pool-1-thread-1 End of taskNumber = 7
    pool-1-thread-5 End of taskNumber = 8
    Finished all threads
    
    
    Refer: 
    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html

    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html

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

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

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/d6d97a164a2acde3c2aa603063db8720e3eaea01/BasicJava/ThreadDemo_ThreadPool_V3_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
  • Kids: Capital of all countries | Capital of countries | Countries and capitals_V8


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

    Click the below Image to Enlarge 
    Kids: Capital of all countries | Capital of countries | Countries and capitals_V8
    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
  • Kids: Capital of all countries | Capital of countries | Countries and capitals_V6


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

    Click the below Image to Enlarge
    Kids: Capital of all countries | Capital of countries | Countries and capitals_V6 
    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
  • Kids: Capital of all countries | Capital of countries | Countries and capitals_V5


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

    Click the below Image to Enlarge
    Kids: Capital of all countries | Capital of countries | Countries and capitals_V5 
    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
  • Kids: Capital of all countries | Capital of countries | Countries and capitals_V4


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=4rTyfXnp8OM&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Kids: Capital of all countries | Capital of countries | Countries and capitals_V4 
    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
  • Google+ Followers

    Google+ Badge