java生产者消费者模式练习

/**
 * 
 */
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值