zoukankan      html  css  js  c++  java
  • 多线程(2)

    1jdk中线程是协作式的;os中线程是抢占式的
    2hashcode可以看作对象在内存中的值,但并不是,system.identityhashcode是对象的原值
    3i++造成线程不安全是因为 重新声明了对象,导致锁住的不是同一个对象
    4volatile只是保证了可见性问题,static是让以这个类对象创建的对象可以看到这个变量
    5threadlocal线程副本变量,以线程隔离,
    (1)oom看那个图
    (2)remove方法改正
    6sychronized wait notify
    (1)生产者消费者问题,注意锁住一个不变对象
    (2)通过while(true){不满足条件的 wait,满足条件的 notify}
    (3)wait释放锁资源
    7gc泄露,分配大小不够用内存溢出

    生产者消费者问题:开枪 加子弹,只用notify,wait,
    package cn.enjoyedu.ch1.wn;
    
    /**
     * 类说明:
     */
    public class Gun_b {
        private Integer zd=0;
        public synchronized void put(){
            while(zd>=20) {
                try {
                    System.out.println(Thread.currentThread().getName() + " 发现子弹已经装满了,当前子弹剩余=" + zd);
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            zd++;
            System.out.println(Thread.currentThread().getName() + " 装入子弹一枚,当前子弹剩余=" + zd);
            notifyAll();
        }
    
        public synchronized void get(){
            while (zd<=0) {
                try {
                    System.out.println(Thread.currentThread().getName() + " 发现子弹已经射完了,当前子弹剩余=" + zd);
                    wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            zd--;
            System.out.println(Thread.currentThread().getName() + " 发射一枚子弹,当前子弹剩余=" + zd);
            notifyAll();
        }
        static class Commer implements Runnable{
            Gun_b gun;
            public Commer(Gun_b gun){
                this.gun =gun;
            }
            @Override
            public void run() {
                while(true) {
                    gun.get();
                    try {
                        Thread.sleep(12);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    
        }
        static class Product implements Runnable{
            Gun_b gun;
            public Product(Gun_b gun){
                this.gun =gun;
    
            }
            @Override
            public void run() {
                while (true) {
                    gun.put();
                    try {
                        Thread.sleep(6);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            Gun_b gun = new Gun_b();
            for(int i =0;i<6;i++) {
                new Thread(new Product(gun),"生产者"+i).start();
            }
            for(int i =0;i<6;i++) {
                new Thread(new Commer(gun),"消费者"+i).start();
            }
        }
    }
    
    
  • 相关阅读:
    hadoop面试
    常用脚本
    1.kafka
    2.flink
    java面试
    Hbase 项目
    linux 2>&1 和 &的意思
    Jetbrains系列产品重置试用方法
    解决GitHub网页githubusercontent地址无法访问问题
    stylus 成熟的写法
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/13028208.html
Copyright © 2011-2022 走看看