Tuesday 27 December 2016

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


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

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

public class ThreadPoolTest
{

    public static void main(String[] args)
    {
        int numRunnableTask = 4;
        int threadPoolSize = 2;

        ExecutorService tpes = Executors.newFixedThreadPool(threadPoolSize);

        RunnableTask[] runnableTasks = new RunnableTask[numRunnableTask];
        for (int i = 0; i < numRunnableTask; i++)
        {
            runnableTasks[i] = new RunnableTask(i);
            tpes.execute(runnableTasks[i]);
        }
        tpes.shutdown();
    }
}
RunnableTask.java
/*
 * Here is a runnable task. This task performs some work and
 * then periodically reports what percent of the work it has
 * completed:
 */

public class RunnableTask implements Runnable
{
    private int taskNumber;

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

    public void run()
    {
        for (int i = 0; i <= 100; i += 20)
        {
            // Perform some work ...
            System.out.println(Thread.currentThread().getName()
                    + " = RunnableTask number: " + taskNumber
                    + ", percent complete: " + i);
            try
            {
                Thread.sleep(3000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}
Output
pool-1-thread-1 = RunnableTask number: 0, percent complete: 0
pool-1-thread-2 = RunnableTask number: 1, percent complete: 0
pool-1-thread-1 = RunnableTask number: 0, percent complete: 20
pool-1-thread-2 = RunnableTask number: 1, percent complete: 20
pool-1-thread-2 = RunnableTask number: 1, percent complete: 40
pool-1-thread-1 = RunnableTask number: 0, percent complete: 40
pool-1-thread-1 = RunnableTask number: 0, percent complete: 60
pool-1-thread-2 = RunnableTask number: 1, percent complete: 60
pool-1-thread-1 = RunnableTask number: 0, percent complete: 80
pool-1-thread-2 = RunnableTask number: 1, percent complete: 80
pool-1-thread-1 = RunnableTask number: 0, percent complete: 100
pool-1-thread-2 = RunnableTask number: 1, percent complete: 100
pool-1-thread-2 = RunnableTask number: 2, percent complete: 0
pool-1-thread-1 = RunnableTask number: 3, percent complete: 0
pool-1-thread-2 = RunnableTask number: 2, percent complete: 20
pool-1-thread-1 = RunnableTask number: 3, percent complete: 20
pool-1-thread-1 = RunnableTask number: 3, percent complete: 40
pool-1-thread-2 = RunnableTask number: 2, percent complete: 40
pool-1-thread-1 = RunnableTask number: 3, percent complete: 60
pool-1-thread-2 = RunnableTask number: 2, percent complete: 60
pool-1-thread-2 = RunnableTask number: 2, percent complete: 80
pool-1-thread-1 = RunnableTask number: 3, percent complete: 80
pool-1-thread-1 = RunnableTask number: 3, percent complete: 100
pool-1-thread-2 = RunnableTask number: 2, percent complete: 100
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_V2_App.zip?attredirects=0&d=1

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

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