The Artima Developer Community
Sponsored Link

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

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V5) Posted: Dec 30, 2016 4:22 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V5)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

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
  • Read: Java Tutorial: Java Threads (Thread pool in java | Java thread pool | Java thread pool tutorial_V5)

    Topic: 2016 average total return of IPOs was 23%, $15.2bn in tech M&A Previous Topic   Next Topic Topic: Kids: Health, Household and Personal Care_V8

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use