0

重複の可能性:
スレッドを順番に実行するマルチスレッドJavaクラスのテスト

以下の質問を重複したものとして扱わないでください。

マルチスレッドを一度に1つずつ順番に順番に実行できるクラスを開発しました。このクラスのclaimAccess関数とreleaseAccess関数の間​​のすべてのアプリケーションコードは、一度に1つのスレッドでのみ実行されます。他のすべてのスレッドは、前のスレッドが完了するまでキューで待機します。main()メソッド自体にコードを記述して、クラスをテストすることをお勧めします。

import java.util.ArrayList;
import java.util.List;

public class AccessGate {
    protected boolean shouldWait = false;
    protected final List waitThreadQueue = new ArrayList();

    /**
     * For a thread to determine if it should wait. It it is, the thread will
     * wait until notified.
     * 
     */
    public void claimAccess() {
        final Thread thread = getWaitThread();
        if (thread != null) {
            // let the thread wait untill notified
            synchronized (thread) {
                try {
                    thread.wait();
                } catch (InterruptedException exp) {
                }
            }
        }
    }

    /**
     * For a thread to determine if it should wait. It it is, the thread will be
     * put into the waitThreadQueue to wait.
     * 
     */
    private synchronized Thread getWaitThread() {
        Thread thread = null;
        if (shouldWait || !waitThreadQueue.isEmpty()) {
            thread = Thread.currentThread();
            waitThreadQueue.add(thread);
        }
        shouldWait = true;
        return thread;
    }

    /**
     * Release the thread in the first position of the waitThreadQueue.
     * 
     */
    public synchronized void releaseAccess() {
        if (waitThreadQueue.isEmpty()) {
            shouldWait = false;
        } else {
            shouldWait = true;
            // give the claimAccess function a little time to complete
            try {
                Thread.sleep(10);
            } catch (InterruptedException exp) {
            }

            // release the waiting thread
            final Thread thread = (Thread) waitThreadQueue.remove(0);
            synchronized (thread) {
                thread.notifyAll();
            }
        }
    }
}

今私の主な方法は..

public static void main (String args[])
{

}

上記のクラスをテストするために、メインメソッドでスレッドを生成する方法を教えてください..!!アドバイスしてください

4

2 に答える 2

1

これで始められるはずです...

public static void main (String args[])
{
    AccessGate gate = new AccessGate();

    // create as many threads as you like
    Thread t1 = new MyThread(gate);
    Thread t2 = new MyThread(gate);

    // start all the threads you created
    t1.start();
    t2.start();        
}

class MyThread extends Thread {

    AccessGate gate;

    public MyThread(AccessGate g) {
        gate = g;
    }

    public void run() {
        gate.claimAccess();
        // Do something or print something.
        // Could output several statements.
        // Why not do a sleep as well to see if other threads interrupt
        // this code section.
        gate.releaseAccess();
    }
}
于 2012-12-09T07:11:15.600 に答える
0

の使用を検討してくださいExecutors.newSingleThreadExecutor()。これは、タスクを実行するスレッドが1つだけのスレッドプールです。次のタスクは、最初のタスクが終了した後にのみ実行を開始します。

Executor executor = Executors.newSingleThreadExecutor();
Future<String> future1 = executor.submit(new Callable<String>() {
    @Override
    String call() throws Exception {
        // my first task
    }
});
Future<String> future2 = executor.submit(new Callable<String>() {
    @Override
    String call() throws Exception {
        // my second task
    }
});
...

Future APIを介してタスク実行の結果を取得できます。また、各ジョブのステータスを追跡することもできます。

于 2012-12-09T13:32:45.213 に答える