• 每日总结


    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();
        }
    }

  • 相关阅读:
    Qt5信号与槽新写法
    Qt获取当前时间
    奇妙的enum class,enum struct组合
    vs2010+qt4编译出现error LNK2001: 无法解析的外部符号 "public: virtual struct QMetaObject等错误
    QTreewidget的使用
    Qt各版本,VS插件下载地址
    Qt按钮设置透明
    Qt全局坐标和相对坐标
    QTableWidget
    c++11中thread join和detach的区别
  • 原文地址:https://www.cnblogs.com/yitiaokuailedexiaojingyu/p/13982881.html
Copyright © 2020-2023  润新知