zoukankan      html  css  js  c++  java
  • 每日总结

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

  • 相关阅读:
    extract numberic from text
    str.rfind("//")
    solr入门心得
    深挖洞,广积粮,缓称王
    ubuntu 查看文件夹大小

    关于托运
    iconv转换文件编码
    小毛小毛你的心态
    广积粮,高筑墙,缓称王
  • 原文地址:https://www.cnblogs.com/yitiaokuailedexiaojingyu/p/13982881.html
Copyright © 2011-2022 走看看