-1

If I create 1K threads and launch them at the same time using a latch, once the threads complete my process ends.

What I want to do is, as the thread ends, start up another thread to work on the same task (or somehow get the same thread to continue processing with the same task again).

Scenario:

I want to start 1K threads, and don't want the performance penalty of starting another 1K threads when they finish processing.

The threads simply make a http url connection to a url http://www.example.com/some/page

What I want to do is continuously run for x seconds, and always have 1K threads running.

I don't want to use an executor for this for both learning how to do it w/o it and I believe since the executor framework separates the task and threads, it doesn't gaurantee how many threads are running at the same time.

codecompleting
  • 9,251
  • 13
  • 61
  • 102
  • 9
    It is never a good idea to create 1,000 threads. – SLaks Dec 29 '11 at 20:41
  • @SLaks Its for benchmarking, I want to learn how to create a benchmarking tool. – codecompleting Dec 29 '11 at 20:51
  • 1
    Creating that many individual threads is probably not doing what you think it is, and there are probably better ways to have 1000 concurrent requests running. Note that you are spinning up 1000 threads that are spending the majority of their time *waiting*. – Andrew Barber Dec 29 '11 at 21:01
  • 1
    ...it's a bit like going to the bank with ten checks to deposit into your account, and bringing ten friends, each of whom deposits a single check. – Andrew Barber Dec 29 '11 at 21:04
  • @AndrewBarber so your saying a server cannot run 1K threads at the same time? – codecompleting Dec 29 '11 at 22:09
  • I'm saying you don't understand threading, if you are creating 1000 threads to run 1000 concurrent network calls. Your threads are spending most of their time doing nothing (waiting for network response), and you could use some sort of async call with far fewer threads, instead. – Andrew Barber Dec 30 '11 at 01:24
  • @codecompleting you can start a 1000 threads, buth the OS can only execute one thread per CPU core concurrently, and will pause and resume threads according to some scheduling policy. On Linux, it ranges between 10-200ms. The scheduler needs to keep track of what each thread is doing to decide to which thread to switch to next. At 1000 threads, the scheduler can take longer to switch between threads than the threads can actually execute. See http://www.javamex.com/tutorials/threads/how_threads_work.shtml – GeertPt Dec 30 '11 at 12:57

3 Answers3

2

You'll have to do it in the Runnable itself. Create a simple loop surrounding your actions. If you want them all to synchronize at a certain point, create a CountdownLatch with count 1000 and at the end of every iteration do a countDown and await.

Apache JMeter is a free performance testing tool that you can easily configure to test URL's in multiple threads. It can also distribute the tests to have e.g. 10 clients doing 100 threads instead.

GeertPt
  • 16,398
  • 2
  • 37
  • 61
1

use a loop in your run() method.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
1

Close as I can tell, you want to have a large number of server threads, and have them execute a piece of work from a list, then come back and wait for another piece of work to be be specified (or work on another already-present piece in the list).

This is what you use a queue for. Probably a BlockingQueue is the simplest form to use that will suit your purposes, and there are several implementations of this in the JDK.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151