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关键字使用小结