0

サーバーに接続している複数のクライアントをエミュレートして、クライアント/サーバーシステムのストレステストを試みています。クライアントごとにスレッドがあります。しかし、次のコードを実行すると(ClientStartEmulator()はクライアントを表します)、スレッドは同時にではなく順次実行されます。(複数のスレッドが生成され、エミュレートされた各クライアント内でスリープしているにもかかわらず)。何が悪いのか考えてみませんか?

別の方法は、各jarに対してシステムコールを実行することですが、これは煩わしいものです(ここには示されていません)。返された配列に対していくつかの処理を実行します。

ありがとう!

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(); 

    }
}
4

2 に答える 2

3

client.start()バックグラウンドでスレッドを開始する呼び出しを行う必要があります。呼び出すclient.run()ことにより、代わりに呼び出し元のスレッドで run メソッドを実行しています。あなたが望むものではないと思います。

Threadコードから:

/**
 * 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() {
   ...

メソッドはstart()ネイティブ スレッドを作成し、新しいスレッドがThread.run()メソッドを呼び出す間に戻ります。

于 2012-04-10T15:16:32.937 に答える
0

start新しいスレッドに置き換える run代わりに so を呼び出すと、実行可能または実行中の状態に移動しますclient.run()client.start()

于 2012-04-10T15:36:24.143 に答える