• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread pool how to

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all

can someone please point me to Thread pool tutorial

i need simple thing to be able to define the Thread pool count

and the task count

im using java 1.5
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/tutorial/essential/concurrency/pools.html

One simple example is

Which creates a thread pool with 10 threads.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm its not easy to understand how to use it for what i need
can i find some kind of example or tutorial please
thanks
 
Henry Wong
author
Sheriff
Posts: 22541
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:
hmm its not easy to understand how to use it for what i need
can i find some kind of example or tutorial please
thanks


Take a look at the tutorial again -- it is actually incredibly easy. There is a convenience factory class, the Executors class, which is used to generate a couple of different common types of executors (of which a thread pool is one). Just pick the one that Jim used.

A thread pool is represented as an ExecutorService. Again, as shown in Jim's example.

The ExecutorService has a bunch of methods that execute tasks. Just take a look at the invoke() and submit() methods for more details.

As for the tasks themselves, you may use the previous common runnable interface, or the newly created callable interface. There are many examples in both the tutorial and the JavaDoc for the ExecutorService.

Henry
[ January 11, 2007: Message edited by: Henry Wong ]
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the fats reply
i did found good and simple tutorial
http://bioportal.weizmann.ac.il/course/prog2/tutorial/essential/threads/pool.html
only one parameter i missing how to control that each worker will be called every 2 seconds
and under small thing i what to learn is why do i need the Thread.sleep
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All right, it looks like the latest Sun tutorial doesn't provide any example code using Executors. Still, I think that JDK 5's new classes will ultimately be simpler to use than the old classes. Here is an article with more detailed examples of how to use executors. And here is another.

[Meir Yan]: why do i need the Thread.sleep

In that tutorial, Thread.sleep() is just as an example - they wanted to simulate a task that does something, that takes some time to perform. Maybe it's performing a complex calculation, or running a database query, or waiting for a user to send data on a socket connection. Could be anything. Thread.sleep() is just one simple way to create an artificial delay.

[Meir Yan]: how to control that each worker will be called every 2 seconds

Well there are several ways to do that, but maybe you should first tell us more about what you're doing. What types of tasks are you executing? Why is "every 2 seconds" important? Typically with a thread pool, you would simply submit tasks to the pool, and the pool would execute them as soon as one of its threads becomes available. Why wait 2 seconds if you don't have to?
[ January 11, 2007: Message edited by: Jim Yingst ]
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
again thanks for the fast reply
ok my task is this ( im new to java Threads so its somekind of new stuff for me )
i need to simulate methods calls ( Corba API Calls ) like 10 call every call will dispatch to the Threadpool every 2 seconds The pool with contain 5 Threads .
i will like to print the latency between Thread That is when Thread n started and when this Thread in done
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me guess a bit at what you're doing. It sounds like one part of your program generates a request every 2 seconds, simulating a CORBA client, for a total of 10 requests. The other part processes requests on as many as 5 threads, simulating a server. If processes average just under 10 seconds, the processor will pretty much keep up; the 5 threads will be mostly busy but no requests will be waiting for processing time. If processes average more than 10 seconds, requests may stack up in the queue inside the thread pool.

I'm not sure which times you are interested in. I'd want to know when a request is submitted, when it is started and when it is completed. And the total time for my 10 requests.

Does that sound about right?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok here is my program it is simple , but there is some things im not understand yet

my main class



and here is my worker thread class



now this code works but i dont understand how can get the latency between each thread that has bean invoked also how can i be sure the threads that has bean invoked are done and not idle someware the futures[i].get() suposed
to give me this ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!