Java notify() and notifyAll() test

5219 단어 자바thread
프로그램 은 20 개의 스 레 드 를 시작 합 니 다. 20 개의 스 레 드 는 모두 같은 sync 의 object (이름 은 SYNC) 를 사용 합 니 다.스 레 드 시작 코드 는 다음 과 같 습 니 다:
final TC[] ts = new TC[20];
        for (int i = 0; i < ts.length; i++) {
            TC target = new TC("TC " + i, SYNC1);
            Thread thread = new Thread(target);
            ts[i] = target;
            thread.start();
}

------------------------------------------------------------
다음 에 바로 다른 스 레 드 를 시작 하여 notify 작업 에 사용 합 니 다.

//        Thread
        new Thread(new Runnable() {
            public void run() {
                synchronized (SYNC1) {
                    int i = 10;
                    while (i > 0) {
                        System.out.println("Now will notify the thread " + i);
                        ts[i].notifySelf();
                        try {
                            SYNC1.wait(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        i--;
                    }
                }
            }
 }).start();

------------------------------------------------------------

   TC      :

class TC implements Runnable {

    private final String name;

    private final Object sync;

    private boolean isRunning = false;

    public TC(String name, Object sync) {
        this.name = name;
        this.sync = sync;
    }

    public void run() {
        synchronized (sync) {
            while (true) {
                //               。                   ,       。
                if (!isRunning) {
                    try {
                        sync.wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    isRunning = true;
                }
                System.out.println(name + " Running .......");
                try {
                    sync.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }// Wait 1 second
            }
        }
    }

    public void notifySelf() {
        synchronized (sync) {
            System.out.println("Coming to notify the thread " + name);
            sync.notify();
        }

    }

}

분할 선
출력 로 그 는 다음 과 같 습 니 다 (/ 부분 은 제 가 추가 한 주석 입 니 다. 실제 출력 이 아 닙 니 다):
Now will notify the thread 10 / / 먼저 notify 10 번 째 TC Coming to notify the thread TC 10 / / 도 SYNC 의 notify 작업 을 호출 했 지만 아직 wait 에 스 레 드 가 없 기 때문에 이 notify 신 호 는 버 려 졌 습 니 다.이 notify 가 지나 가면 지나 갑 니 다.뒤의 어떤 조작 에 도 영향 을 주지 않 을 것 이다.TC 1 Running ....... TC 5 Running ....... TC 0 Running ....... TC 2 Running ....... TC 4 Running ....... TC 6 Running ....... TC 8 Running ....... TC 10 Running ....... TC 12 Running ....... TC 14 Running ....... TC 7 Running ....... TC 9 Running ....... TC 11 Running ....... TC 13 Running ....... TC 15 Running ....... TC 17 Running ....... TC 3 R...................................................................................................처음부터 끝까지 1 - 20 의 시작 이 아니 라 log 가 1, 5, 0, 2, 4, 6, 8, 10, 12, 14 를 표시 합 니 다.. wait 의 순 서 는 1, 5, 0, 2, 4, 6, 8, 10, 12, 14...... Now will notify the thread 9 Coming to notify the thread TC 9 TC 1 Running........ Now will notify the thread 8 Coming to notify the thread TC 8 TC 5 Running...... Now will notify the thread 7 Coming to notify the thread TC 7 TC 0 Running........ Now will notify the thread 6 Coming to notify the thread TC 5 Runningthread TC 6 TC 2 Running ....... Now will notify the thread 5 Coming to notify the thread TC 5 TC 4 Running ....... Now will notify the thread 4 Coming to notify the thread TC 4 TC 6 Running ....... Now will notify the thread 3 Coming to notify the thread TC 3 TC 8 Running ....... Now will notify the thread 2 Coming to notify the thread TC 2 TC 10Running..... Now will notify the thread 1 Coming to notify the thread TC 1 TC 12 Running.그들 은 pool 에 들 어 가 는 순서에 따라 줄 을 서서 notify 에 게 주 었 다.하나의 notify 작업 은 wait 대기 열의 첫 번 째 스 레 드 만 활성화 합 니 다.일대일 조작.
분할 선
notify 대신 notify All 을 사용 합 니 다.
로그 출력:
Now will notify the thread 10 Coming to notify the thread TC 10 / / 첫 번 째 notify All 작업 은 TC 0 Running 을 버 렸 습 니 다.C 2 Running..... TC 4 Running..... TC 6 Running..... TC 8 Running.TC 0 Running ....... TC 1 Running ....... TC 3 Running ....... TC 5 Running ....... TC 7 Running ....... TC 9 Running ....... TC 11 Running ....... TC 2 Running ....... TC 13 Running ....... TC 15 Running ....... TC 4 Running ....... TC 17 Running ....... TC 19 Running ....... TC 6 Running ....... TC 8 Running ....... TC 10 Running ....... TC 12 Running ....... TC 14 Running ....... TC 16 Running ....... TC 18 Running .......

좋은 웹페이지 즐겨찾기