Friday, 30 December 2016

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
  • No comments:

    Post a Comment