• Thread Object wait() notify()基本


    package com.thread.test.thread;
    
    import java.util.ArrayDeque;
    import java.util.Queue;
    import java.util.concurrent.ThreadLocalRandom;
    
    /**
     * Created by windwant on 2016/11/29.
     */
    public class MyQueueSyn {
    
        public static void main(String[] args) throws InterruptedException {
            CR cr = new CR();
            cr.addEle(String.valueOf(ThreadLocalRandom.current().nextInt(100)));
            cr.start();
    
            new CRP(cr).start();
    
    //        for (int i = 0; i < 100; i++) {
    //            Thread.sleep(1000);
    //            cr.addEle(String.valueOf(i * i));
    //            if(i > ThreadLocalRandom.current().nextInt(100)) {
    //                cr.tify();
    //            }
    //            System.out.println("mian thread add cr queue ele: " + i);
    //        }
        }
    }
    
    class CRP extends Thread{
    
        private CR cr;
    
        public CRP(CR t){
            cr = t;
        }
    
        @Override
        public void run() {
            for (int i = 0; i < 100; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                cr.addEle(String.valueOf(i * i));
                if(i > ThreadLocalRandom.current().nextInt(100)) {
                    cr.tify();
                }
                System.out.println("mian thread add cr queue ele: " + i);
            }
        }
    }
    
    class CR extends Thread{
    
        public void addEle(String ele) {
            synchronized (queue) {
                queue.add(ele);
            }
        }
    
        public void tify(){
            synchronized (queue){
                queue.notify();
            }
        }
    
        public Queue<String> queue = new ArrayDeque<>();
    
        @Override
        public void run() {
            while (true){
                synchronized (queue){
                    try {
                        if(queue.size() == 0){
                            System.out.println("cr thread queue wait...");
                            queue.wait();
                        }
                        Thread.sleep(1000);
                        System.out.println("cr thread queue poll ele: " + queue.poll());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
  • 相关阅读:
    TCP 基础知识
    Spring Boot 实战 —— 日志框架 Log4j2 SLF4J 的学习
    MySQL 实战笔记
    Java 基础
    RPM 包的构建
    RPM 包的构建
    9. 桶排序
    8. 基数排序
    7. 计数排序
    6. 快速排序
  • 原文地址:https://www.cnblogs.com/niejunlei/p/6114435.html
Copyright © 2020-2023  润新知