天天看點

多線程3

public class MyContainer1 {

List lists = new ArrayList();

public void add(Object o) {
    lists.add(o);
}

public int size() {
    return lists.size();
}

public static void main(String[] args) {
    MyContainer1 c = new MyContainer1();

    new Thread(() -> {
        for(int i=0; i<10; i++) {
            c.add(new Object());
            System.out.println("add " + i);

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }, "t1").start();

    new Thread(() -> {
        while(true) {
            if(c.size() == 5) {
                break;
            }
        }
        System.out.println("t2 結束");
    }, "t2").start();
}
           

}

public class MyContainer2 {

//添加volatile,使t2能夠得到通知
volatile List lists = new ArrayList();

public void add(Object o) {
    lists.add(o);
}

public int size() {
    return lists.size();
}

public static void main(String[] args) {
    MyContainer2 c = new MyContainer2();

    new Thread(() -> {
        for(int i=0; i<10; i++) {
            c.add(new Object());
            System.out.println("add " + i);

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }, "t1").start();

    new Thread(() -> {
        while(true) {
            if(c.size() == 5) {
                break;
            }
        }
        System.out.println("t2 結束");
    }, "t2").start();
}
           

}

public class MyContainer3 {

    //添加volatile,使t2能夠得到通知
    volatile List lists = new ArrayList();

    public void add(Object o) {
        lists.add(o);
    }

    public int size() {
        return lists.size();
    }

    public static void main(String[] args) {
        MyContainer3 c = new MyContainer3();

        final Object lock = new Object();

        new Thread(() -> {
            synchronized(lock) {
                System.out.println("t2啟動");
                if(c.size() != ) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("t2 結束");
            }

        }, "t2").start();

        try {
            TimeUnit.SECONDS.sleep();
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        new Thread(() -> {
            System.out.println("t1啟動");
            synchronized(lock) {
                for(int i=; i<; i++) {
                    c.add(new Object());
                    System.out.println("add " + i);

                    if(c.size() == ) {
                        lock.notify();
                    }

                    try {
                        TimeUnit.SECONDS.sleep();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, "t1").start();


    }
}

           

public class MyContainer4 {

//添加volatile,使t2能夠得到通知
volatile List lists = new ArrayList();

public void add(Object o) {
    lists.add(o);
}

public int size() {
    return lists.size();
}

public static void main(String[] args) {
    MyContainer4 c = new MyContainer4();

    final Object lock = new Object();

    new Thread(() -> {
        synchronized(lock) {
            System.out.println("t2啟動");
            if(c.size() != 5) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t2 結束");
            //通知t1繼續執行
            lock.notify();
        }

    }, "t2").start();

    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

    new Thread(() -> {
        System.out.println("t1啟動");
        synchronized(lock) {
            for(int i=0; i<10; i++) {
                c.add(new Object());
                System.out.println("add " + i);

                if(c.size() == 5) {
                    lock.notify();
                    //釋放鎖,讓t2得以執行
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }, "t1").start();


}
           

}

public class MyContainer5 {

// 添加volatile,使t2能夠得到通知
volatile List lists = new ArrayList();

public void add(Object o) {
    lists.add(o);
}

public int size() {
    return lists.size();
}

public static void main(String[] args) {
    MyContainer5 c = new MyContainer5();

    CountDownLatch latch = new CountDownLatch(1);

    new Thread(() -> {
        System.out.println("t2啟動");
        if (c.size() != 5) {
            try {
                latch.await();

                //也可以指定等待時間
                //latch.await(5000, TimeUnit.MILLISECONDS);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("t2 結束");

    }, "t2").start();

    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

    new Thread(() -> {
        System.out.println("t1啟動");
        for (int i = 0; i < 10; i++) {
            c.add(new Object());
            System.out.println("add " + i);

            if (c.size() == 5) {
                // 打開門闩,讓t2得以執行
                latch.countDown();
            }

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }, "t1").start();

}
           

}