• 每日总结


    2020年11月15日:

    此程序运行时输出来时的“*”号是每个500毫秒进行输出的,这是因为程序在调用start函数时运行时被sleep函数进行了阻拦,进而造成程序被“沉睡”的感觉。

    线程的同步:

     生产者和消费者:

    package xian;
    import java.util.LinkedList;
    import java.util.Queue;
    public class xiao {
        private final int MAX_LEN = 10;
        private Queue<Integer> queue = new LinkedList<Integer>();
        class Producer extends Thread {
            @Override
            public void run() {
                producer();
            }
            private void producer() {
                while(true) {
                    synchronized (queue) {
                        while (queue.size() == MAX_LEN) {
                            queue.notify();
                            System.out.println("当前队列满");
                            try {
                                queue.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        queue.add(1);
                        queue.notify();
                        System.out.println("生产者生产一条任务,当前队列长度为" + queue.size());
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        class Consumer extends Thread {
            @Override
            public void run() {
                consumer();
            }
            private void consumer() {
                while (true) {
                    synchronized (queue) {
                        while (queue.size() == 0) {
                            queue.notify();
                            System.out.println("当前队列为空");
                            try {
                                queue.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        queue.poll();
                        queue.notify();
                        System.out.println("消费者消费一条任务,当前队列长度为" + queue.size());
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        public static void main(String[] args) {
          xiao pc = new xiao();
            Producer producer = pc.new Producer();
            Consumer consumer = pc.new Consumer();
            producer.start();
            consumer.start();
        }
    }

  • 相关阅读:
    iOS开发-UIScrollView原理
    关于tableView点击状态栏列表回到顶部的说明
    实用技术之bugly
    KVO你所不知道的"坑"
    iOS iPhone SDK 包含哪些东西?
    NSTime的全面认识
    四 数据结构与算法总结(一)
    三 数据结构 --数和二叉树
    二 线性表
    一 数据结构的概念,时间复杂度和空间复杂度
  • 原文地址:https://www.cnblogs.com/yitiaokuailedexiaojingyu/p/13982881.html
Copyright © 2020-2023  润新知