java生产者消费者模式练习

来源:互联网 发布:淘宝客推广链接转换 编辑:程序博客网 时间:2024/05/22 03:07
/** *  */package ThreadTest;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @ClassName: ThreadCommunicationTest3 * @Description:TODO * @author 10165212 * @date 2016-2-24 下午6:31:51 */public class ThreadCommunicationTest3 {    private  boolean flag = false;    private Object object = new Object();    public static void main(String[] args) {        ExecutorService service = Executors.newCachedThreadPool();        ThreadCommunicationTest3 test3 = new ThreadCommunicationTest3();        service.execute(test3.new RunnableA());        service.execute(test3.new RunnableB());    }    private class RunnableA implements Runnable {        int i=0;        @Override        public void run() {           while(true){                synchronized (object) {                    try {                        if (flag) {                            object.wait();//该方法运行后线程会阻塞,并释放锁。直到有notify,才会唤醒,处于就绪态。分配到资源后,才会接着向下运行。                        } else {                            System.out.println("i:" + i++);                            //flag = true;写在这里和写在下面一样,因为只有方法运行完才会释放锁                            object.notifyAll();                            flag = true;                        }                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                try {                    Thread.sleep(2000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }    private class RunnableB implements Runnable {        int j=0;        @Override        public void run() {            while(true){                synchronized (object) {                    try {                        if (!flag) {                            object.wait();                        } else {                            System.out.println("j:" + j++);                            flag = false;                            object.notifyAll();                                                    }                    } catch (InterruptedException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                }                try {                    Thread.sleep(2000);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}

/** *  */package ThreadTest;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * @ClassName: ThreadCommunicationTest3 * @Description:两个线程循环间隔打印指定内容,一个打印从1到52的数字,一个打印从A到Z的字母,打印输出如下:12A34B......5152Z * @author 10165212 * @date 2016-2-24 下午6:31:51 */public class ThreadCommunicationTest4 {    private boolean flag = false;    private Object object = new Object();    private char currentChar = 'A';    public static void main(String[] args) {        ExecutorService service = Executors.newCachedThreadPool();        ThreadCommunicationTest4 test4 = new ThreadCommunicationTest4();        service.execute(test4.new RunnableA());        service.execute(test4.new RunnableB());        service.shutdown();    }    private class RunnableA implements Runnable {        @Override        public void run() {            for (char currentChar = 'A'; currentChar <= 'Z'; currentChar++) {                System.out.println("A的下一个循环到来,等待获取锁");                synchronized (object) {                    System.out.println("线程A获取锁");                    try {                        if (!flag) {                            System.out.println("线程A进入wait");                            object.wait();                        }                         System.out.println(currentChar);                        flag = false;                        System.out.println("线程A唤醒所有线程");                        object.notifyAll();                                                //Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                System.out.println("线程A释放锁");            }        }    }    private class RunnableB implements Runnable {        @Override        public void run() {            for (int j = 1; j <= 52; j++) {                System.out.println("B的下一个循环到来,等待获取锁");                synchronized (object) {                    System.out.println("线程B获取锁");                    try {                        if (flag) {                            System.out.println("线程B进入wait");                            object.wait();                        }                        System.out.println(j);                        if (j % 2 == 0) {                            flag = true;                            System.out.println("线程B唤醒所有线程");                            object.notifyAll();                        }                        //Thread.sleep(1000);                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }                System.out.println("线程B释放锁");            }        }    }}

0 0
原创粉丝点击