0

I am trying to stress test a client/server system by emulating multiple clients connecting to the server. There is a thread per client. BUT, when I run the following code (ClientStartEmulator() represents a client), threads run sequentially rather than concurrently. (despite there being multiple thread yields and sleep within each emulated client). Any ideas what is wrong?

An alternative would be to do a system call to each jar, but this would be annoying as (not shown here), I do some processing on the arrays returned.

Thanks!

ClientStartEmulator emu = new ClientStartEmulator();
    emu.start(7777, "localhost", "examplestore", "foobar", "signFiles", "foobar", true, time, max_length); 
    ArrayList results = new ArrayList() ; 
    for (int i = 0 ; i<nb_clients ; i++ ) {
        Thread client = new Thread() {
            public void run() {
                ClientStartEmulator emul = new ClientStartEmulator();
                try {
                    emul.start(7777, "localhost", "examplestore", "foobar",     "signFiles", "foobar", false, time, max_length);

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }
        };
        client.run(); 

    }
}
user1018513
  • 1,682
  • 1
  • 20
  • 42
  • Related to http://stackoverflow.com/questions/3027495/java-thread-run-and-start-methods – Gray Apr 10 '12 at 15:19

2 Answers2

3

You should be calling client.start() which starts the thread in the background. By calling client.run() you are executing the run method in the calling thread instead. Not what you want I assume.

From the Thread code:

/**
 * Causes this thread to begin execution; the Java Virtual Machine 
 * calls the <code>run</code> method of this thread. 
 * <p>
 * The result is that two threads are running concurrently: the 
 * current thread (which returns from the call to the 
 * <code>start</code> method) and the other thread (which executes its 
 * <code>run</code> method). 
 * ...
 */
public synchronized void start() {
   ...

The start() method creates the native thread and returns while the new thread calls the Thread.run() method.

Gray
  • 115,027
  • 24
  • 293
  • 354
0

start calls run so instead of client.run() replace with client.start() the new thread moves to runnable or running state

mykey
  • 575
  • 2
  • 10
  • 24