zoukankan      html  css  js  c++  java
  • 并发的第二天

    lock

    synchronized 和 lock 的区别

    1、synchronized 自动上锁,自动释放锁,Lock 手动上锁,手动释放锁。

    2、synchronized 无法判断是否获取到了锁,Lock 可以判断是否拿到了锁。

    3、synchronized 拿不到锁就会一直等待,Lock 不一定会一直等待。

    4、synchronized 是 Java 关键字,Lock 是接口。

    使用 Lock 锁,就不能通过 wait 和 notify 来暂停线程和唤醒线程,而应该使用 Condition 的 await 和 signal 来暂停和唤醒线程。

    使用高并发juc交替打印a1····z26

    package com.m.thread.juc;
    
    
    import java.util.Arrays;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;
    
    public class Class01 {
    
        public static void main(String[] args) {
            Lock lock = new ReentrantLock();
            Condition condition = lock.newCondition();
    
            //首先准备材料
            char[] chars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
            int [] number = new int[26];
    
            System.out.println(chars.length);
    
            for (int i = 0; i < 26 ; i++) {
                number[i] = i+1;
            }
    
            System.out.println(Arrays.toString(number));
    
            new Thread(()->{
                lock.lock();
                lock.lock();
                for (int i = 0; i < chars.length; i++) {
                    System.out.print(chars[i]);
                    test(condition);
                }
                condition.signal();
                lock.unlock();
                lock.unlock();
            }).start();
    
    
            new Thread(()->{
                    lock.lock();
                    lock.lock();
                    for (int i = 0; i < number.length; i++) {
                        System.out.println(number[i]);
                        test(condition);
                    }
                    condition.signal();
                    lock.unlock();
                    lock.unlock();
            }).start();
    
    
    
        }
    
        public static void test(Condition condition){
            try {
                TimeUnit.MILLISECONDS.sleep(200);
                condition.signal();
                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    

    ConcurrentModificationException

    并发访问异常

    List

    package com.m.thread.juc;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    public class JucList {
        public static void main(String[] args) {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                new Thread(()->{
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //写
                    list.add("a");
                    //读
                    System.out.println(list);
                },String.valueOf(i)).start();
            }
        }
    }
    
    

    解决方案:

    1.用Vector

    package com.m.thread.juc;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Vector;
    import java.util.concurrent.TimeUnit;
    
    public class JucList2 {
        public static void main(String[] args) {
            List<String> list = new Vector<>();
            for (int i = 0; i < 10; i++) {
                new Thread(()->{
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //写
                    list.add("a");
                    //读
                    System.out.println(list);
                },String.valueOf(i)).start();
            }
    
        }
    }
    
    

    2.Collections.synchronizedList

    package com.m.thread.juc;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Vector;
    import java.util.concurrent.TimeUnit;
    
    public class JucList2 {
        public static void main(String[] args) {
            List<String> list = Collections.synchronizedList(new ArrayList<>());
            for (int i = 0; i < 10; i++) {
                new Thread(()->{
                    try {
                        TimeUnit.MILLISECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //写
                    list.add("a");
                    //读
                    System.out.println(list);
                },String.valueOf(i)).start();
            }
    
        }
    }
    
    

    3、JUC:CopyOnWriteArrayList

    package com.m.thread.juc.list01;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Vector;
    import java.util.concurrent.CopyOnWriteArrayList;
    import java.util.concurrent.TimeUnit;
    
    public class JucList2 {
        public static void main(String[] args) {
            List<String> list = new CopyOnWriteArrayList<>();
            for (int i = 0; i < 10; i++) {
                new Thread(()->{
                    try {
                        TimeUnit.MILLISECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //写
                    list.add("a");
                    //读
                    System.out.println(list);
                },String.valueOf(i)).start();
            }
    
        }
    }
    
    

    transient

    Java transient关键字使用小结







  • 相关阅读:
    ActiveMQ_Linux安装(一)
    Jenkins_多项目构建(二):使用Maven聚集关系
    Shell命令_for
    C# 读取Excel
    [ORACLE错误]ORA-00001: unique constraint (...) violated并不一定是数据冲突
    [ORACLE错误]ORA-00054:resource busy and acquire with nowait specified解决方法
    [ORACLE错误]oracle 不能更新 PL/SQL 点击“edit data”报“ these query results are not updateable”
    只有 DBA 才能导入由其他 DBA 导出的文件
    Windows系统安装Oracle 11g客户端
    Windows系统安装Oracle 11g数据库
  • 原文地址:https://www.cnblogs.com/k-class/p/13718422.html
Copyright © 2011-2022 走看看