Hello Guys,

I'm trying to use a Thread Pool Executor in my application and I'm kinda stuck somewhere.

My aim is to create a pool of fixed number of threads (say 10) waiting in the pool to be called for action (say to print hello);

I'm still bad at java, but here is what I have come up with.

public class ThreadPoolExecution
{
	public static void  main(String[] args)
	{
		BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
		ThreadPoolExecutor ex = new ThreadPoolExecutor(4,10,20, TimeUnit.SECONDS,q);
		try
		{
				//ex.execute(null);
		}
		catch(RejectedExecutionException e)
		{
			System.out.println(e.getMessage());
		}	
	}

What I need is to know, how a plain thread is created, how to create a thread pool, and how to use ThreadPoolExecution.

I have searched the forum, but I haven't found any solution which I found useful. Can you please help me out?

you can create your own thread classes by:
a. extend the Thread class (which I would advise against)
b. implement the Runnable interface

what exactly do you mean by a 'Thread Pool Executor'?

I googled ThreadPoolExecutor and immediately found loads of tutorials and examples for the use of this class. If your Google is broken I can forward some of them to you :-/

strange, why would you advise against extending a thread class, and why would you advise me to use implement a runnable interface.

bigger picture: I'm trying to write a thread pool which would be used to develop a pooling mechanism for a web service, which serve at least 10000 requests for 30 seconds.

smaller picture: I should create a small pool to show the demo on a scale model.

ThreadPoolExecutor is a class which contains methods to execute several pooled threads

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ThreadPoolExecutor.html

@James, the example I posted is one I got from google.

Basically, I don't want to copy paste everything. I want to know how a thread is implemented and thread pool in implemented, and how ThreadPoolExecutor can be used to achieve my goal.

implementing runnable will make your thread work just as fine.
the best reason to extend a class, is because you want to adapt/improve the code or functionality of that parent class, but you 're not intending to adjust or improve the code in Thread, you just want to use it, that's why.

next to that: you can only extend one class in Java (except of course the Object class) but you can implement an unlimited amount of interfaces. This is another reason why getting used to implementing an interface will make developing using threads a lot easier on the long run.

OK, joking aside...
If you want to know "how ThreadPoolExecutor can be used to achieve my goal" then the tutorials (as opposed to the sample code) on the web are what you need. It's unlikely that anyone here is going to write a better one for this Thread.
When you say "I want to know how a thread is implemented and thread pool in implemented" what are you asking? Do you want to know about the internal mechanisms of Java threading (not easy) or just how you can implement (a) a simple thread and (b) a simple thread pool (easy)?

ah yes, I want to know how to implement a simple thread and pool.

If, like me, you're the kind of person who learns best by doing, here's what I suggest. It comes down to building the pieces one at a time, mastering each before moving on.
Create a little Runnable that prints "hello", sleeps a couple of secs, prints "goodbye" and finishes.
Create a little test program that instantiates your Runnable and creates a new Thread to run it.
Improve the test program that to include a blocking queue of Runnables and load a few Runnables onto it. Create a new Thread that loops taking Runnables from the queue and running them one at a time until the queue is empty.
Do the same thing but starting a number of instances of that Thread simultaneously. Now you have a simple thread pool implementation.

Do resist the temptation to skip steps. At each step you will learn about what code you need, what exceptions need to be handled etc, and what kind of problems and situations this simple version doesn't handle well. The whole thing shouldn't take more than an hour.
When you have finished you will understand what ThreadPoolExecutor is all about and how much it's doing for you, and you will understand how to use it, and why its constructed that way.

alright this is what I did, I would be posting in 2 different posts to avoid confusion.

simple program.

MainClass.java

package sj;

public class MainClass
{
		MainClass(){}
		public static void main(String[] args)
		{
			RunnerClass runner = new RunnerClass();
			runner.run("this is a test",5,500);
		}
}

RunnerClass.java

package sj;

public class RunnerClass implements Runnable
{
	private String s;
	private int i;
	private long t;
	
	public void run()
	{
		System.out.println("This is a demo");
	}
	
	public void run(String args0,int args1, long args2)
	{
		this.s = args0;
		this.i = args1;
		this.t = args2;
		for(int j=0;j<=i;j++)
		{
			System.out.println(s);
			try
			{	
			 Thread.sleep(t);
			 }
			catch(Exception e)
			{
				System.out.println(e.getMessage());
			}
		}
	}
}

and modified it a bit to this:

MainClass.java

package sj;

public class MainClass
{
		MainClass(){}
		public static void main(String[] args)
		{
			RunnerClass runner = new RunnerClass("this is a test",5,500);
			Thread thread = new Thread(runner);
			thread.start();
		}
}

RunnerClass.java

package sj;

public class RunnerClass implements Runnable
{
	private String s;
	private int i;
	private long t;

	RunnerClass(String args0,int args1, long args2)
	{
		this.s = args0;
		this.i = args1;
		this.t = args2;
	}
	public void run()
	{

		for(int j=0;j<=i;j++)
		{

			System.out.println(s);
			try
			{	
				Thread.sleep(t);
			}
			catch(Exception e)
			{
				System.out.println(e.getMessage());
			}
		}
	}
}

simple as it is. Now is this the right way to do it? or am I doing it wrong?

also I have no clue about blocking queues and how to use them. so im basically stuck again!

That looks OK - test it with more than 1 runnable/thread. Does it run as you expect?

Blocking Queues:
Its a queue - a first-in first-out list. You can put things into it from anywhere, any time you want.
You have a thread that takes the first item from the queue and processes it, then takes the next item etc. If there are no items on the queue the take method blocks (waits) until someone else puts an item on the queue. There's some housekeeping to do around how you shut it all down when the program wants to exit, but basically that's it - just 2 methods: queue.add(aRunnable) and queue.take()
This tutorial looks OK: http://www.javamex.com/tutorials/blockingqueue.shtml

thanks for the pointers james, They're a bit helpful.

With the help of my work colleague I coded a thread pool with 3 classes, pooler, runner and main where runner executes a line of code when called. pooler creates a threadpool with 4 threads and main has main function which calls the thread pool with runners instance.

This works fine but when I decided to monitor the usage using JConsole,it reports that the application uses 14 threads. Not sure why. I suspect that Eclipse is interfering here. So I decided to compile and run from command line and stuick with silly error now.

my project is structured as sj.threadpool, so my package name is threadpool and my files are in workspace/sj/src/threadpool when I compile the java files, the classes gets created, but when I run it I get an error

Exception in thread "main" java.lang.NoClassDefFoundError: MainClass (wrong name
: threadpool/MainClass)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: MainClass.  Program will exit.

anyoe know why the compiler cant find my MainClass?

It's not the compiler that can't find them, its the runtime. It seems it can't find the .class file, maybe because it's not in a folder called threadpool in the classpath?

they're in the same folder, but for some unknown reason the configuration on my system has gone haywire. i reinstalled the java and created this:

I have a class (say its 1st class) which invokes the superclass and creates a thread pool and I set the properties like poolsize, keepalive, etc and list in the same class.

im calling the method of queue.add(new 3rdclass()) from a 2nd class.
i have seen many methods like afterExecute, beforeExecute, etc, etc. I have no clue how teh right method of execution should be here.

what do i do next?

phew,

I finally figured out how to work this thing out. Its still mind numbing because of the lack of proper tutorials on the internet.

anyways, here is what I came up with.

MainClass.java

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainClass {

	public static void main(String[] args) {

		ExecutorService exec = Executors.newFixedThreadPool(3);

		exec.execute(new RunnerClass("Thread#1"));

		exec.execute(new RunnerClass("Thread#2"));

		exec.execute(new RunnerClass("Thread#3"));
		System.out.println("\nShutting Down Service");
		exec.shutdown();
	}
}

RunnerClass.java

public class RunnerClass implements Runnable {

	private String name;
	
	public RunnerClass(String args) {
		this.name = args;
	}
	
	public void run() {
		try{
			System.out.printf("The Thread '%s' is Running",name);
			Thread.sleep(5000);
			System.out.printf("The Thread '%s' is Awake",name);
		}
		catch (Exception e){
			e.printStackTrace();
		}	
	}
}

sorry for the late reply, btw.

work work work+getbulliedbyprojectmanager work work+drink drink+hangover drink+relax pattern is really tiresome :P

That's typical of a lot of the Java API - it's a real saga to find out how to use it, but when you finally get it nailed, it turns out that the designers did know what they were doing, and the solution is really simple. If only the guys who do the documentation were as good as the people who designed the APIs!

Anyway, thanks for sharing your solution - I'm sure many people will learn from it, especially if you mark this thread "solved"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.