再学習JAVA基礎(七):スレッドのwait、notify、notifyAll、sleep

8974 ワード

/**

 *  thread wait  notify  notifyAll  sleep  Interrupted

 * @author tomsnail

 * @date 2015 4 20   3:20:44

 */

public class Test1 {

    

    /**

     *  

     * @author tomsnail

     * @date 2015 4 20   3:14:13

     */

    private static final Object lockObject = new Object();

    

    /**

     *  

     * @author tomsnail

     * @date 2015 4 20   3:14:22

     */

    static class Thread1 implements Runnable{



        @Override

        public void run() {

            synchronized (lockObject) {

                try {

                    System.out.println(Thread.currentThread().getName()+"wait start");

                    lockObject.wait();

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

                System.out.println(Thread.currentThread().getName()+"wait end");

            }

        }

        

    }

    

    /**

     *  

     * @author tomsnail

     * @date 2015 4 20   3:14:36

     */

    static class Thread2 implements Runnable{



        @Override

        public void run() {

            synchronized (lockObject) {

                lockObject.notify();

                System.out.println(Thread.currentThread().getName()+"notify");

            }

        }

    }

    

    /**

     *  

     * @author tomsnail

     * @date 2015 4 20   3:14:51

     */

    static class Thread3 implements Runnable{



        @Override

        public void run() {

            synchronized (lockObject) {

                lockObject.notifyAll();

                System.out.println(Thread.currentThread().getName()+"notifyAll");

            }

        }

    }

    

    /**

     *  

     * @author tomsnail

     * @date 2015 4 20   3:20:30

     */

    static class Thread4 implements Runnable{



        @Override

        public void run() {

            try {

                System.out.println(Thread.currentThread().getName()+"sleep");

                Thread.currentThread().sleep(20000);

            } catch (InterruptedException e) {

                System.out.println(Thread.currentThread().getName()+"Interrupted");

            }

            

        }

        

    }



    public static void main(String[] args) {

        Thread t1 = new Thread(new Thread1());

        Thread t3 = new Thread(new Thread1());

        Thread t4 = new Thread(new Thread1());

        Thread t2 = new Thread(new Thread2());

        Thread t5 = new Thread(new Thread3());

        //3 

        t1.start();

        t3.start();

        t4.start();

        try {

            Thread.currentThread().sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        // 

        t2.start();

        try {

            Thread.currentThread().sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        // 

        t5.start();

        // 

        Thread t6 = new Thread(new Thread4());

        t6.start();    

        try {

            Thread.currentThread().sleep(2000);

        } catch (InterruptedException e) {

            e.printStackTrace();

        }

        // 

        t6.interrupt();

    }



}

結果
Thread-0wait start

Thread-2wait start

Thread-1wait start

Thread-3notify

Thread-0wait end

Thread-4notifyAll

Thread-1wait end

Thread-2wait end

Thread-5sleep

Thread-5Interrupted