2021.09.15

2021.09.15

  • 泛型基础:
  • 集合概述:集合在开发中起到的承载作用
  • 集合继承结构图:
  • 线程同步:synchronized锁机制
  • 线程死锁问题:

泛型基础:

package o0914;

import java.util.ArrayList;

public class f01 {
     
    public static void main(String[] args) {
     
        /*
        不使用泛型 程序员必须知道存储每个数据结构的数据类型
        否则很容易发生ClassCastException
        泛型提供了类似编译器的检测机制
         */
        ArrayList<String> list = new ArrayList<>();
        list.add("java");
        list.add("Linux");
        list.add("JavaScript");

        for (int i = 0;i<list.size();i++) {
     
            Object o1 = list.get(i);
            System.out.println(o1);
        }
    }
}

集合概述:集合在开发中起到的承载作用

  • 假设连接数据库,数据库当中有10条记录,将10条数据查询出来,java会把10条记录封装成java对象,让后将对象放到某个集合当中,将数据传给前端,遍历数据,一一展现出来
  • 集合中存储的不是基本数据类型,也不是java对象,集合中存储的都是java对象的内存地址,或者说存储的是引用,集合里面同样可以套集合
  • 不同的集合,底层都会对应不同的数据结构;集合可分为两大类,一个是以单个方式存储元素,一个是以键值对方式存储;单个存储的超级父类接口:java.util.Collection;以键值对的方式存储元素,这一类的超级父类接java.util.Map;
list.add(99); // 自动装箱Integer

集合继承结构图:

  • Collection继承结构图
    2021.09.15_第1张图片
  • Map继承结构图
    2021.09.15_第2张图片

2021.09.15_第3张图片

线程同步:synchronized锁机制

  • 缺陷:一个方法声明为synchronized,会影响效率
  • 会传一个参数,叫做同步监视器,同步监视器的执行过程:
    2021.09.15_第4张图片
package ksdxc.d19;
// 线程不安全案例:线程不安全,有负数,解决方法:加synchronized关键字
public class d19 {
     
    public static void main(String[] args) {
     
        BuyTicket customer = new BuyTicket();

        new Thread(customer,"a").start();
        new Thread(customer,"b").start();
        new Thread(customer,"c").start();
    }
}

class BuyTicket implements Runnable{
     
    private int ticketNum = 10;
    private boolean flag = true;

    @Override
    public void run() {
     
        while (flag){
     
            try {
     
                buy();
            } catch (InterruptedException e) {
     
                e.printStackTrace();
            }
        }
    }
    // 同步方法,保证线程的安全
    public void synchronized buy() throws InterruptedException{
     
        if (ticketNum <= 0){
     
            flag = false;
            return;
        }
        // 延时
        Thread.sleep(120);
        System.out.println(Thread.currentThread().getName()+""+ticketNum);
        ticketNum--;
    }
}
// 输出:a10 c9 b8 b7 b6 c5 a4 c3 b2 c1
package ksdxc.d19;
// 线程不安全的情况2:取款问题
public class bank {
     

    public static void main(String[] args) {
     
        Account account = new Account(66, "pool");

        Withdrawal withdrawal1 = new Withdrawal(account, 55, "coding");
        Withdrawal withdrawal2 = new Withdrawal(account, 55, "walking");

        withdrawal1.start();
        withdrawal2.start();
    }
}

/*
账户类
 */
class Account {
     
    int remainMoney; // 余额
    String cardName; // 银行卡类别

    public Account(int remainMoney, String cardName) {
     
        this.remainMoney = remainMoney;
        this.cardName = cardName;
    }
}

/*
银行类
 */
class Withdrawal extends Thread {
     
    Account account;
    int drawMone; // 取了多少钱
    String threadName; // 现金

    /*
    构造方法
     */
    public Withdrawal(Account account, int drawMone, String threadName) {
     
        super(threadName);
        this.account = account;
        this.drawMone = drawMone;
    }

    /*
    取钱
     */
    @Override
    public void run() {
     
        /*
        锁account 一定要锁变化的量
        哪个类的属性会发生变化,就锁哪个类的对象
         */
        synchronized (account) {
     
            // 判断有没有前
            if (account.remainMoney - drawMone < 0) {
     
                System.out.println(Thread.currentThread().getName() + "operator fail");
                return;
            }
            try {
     
                Thread.sleep(120);
            } catch (InterruptedException e) {
     
                e.printStackTrace();
            }
            // 程序能够执行到这里说明取款成功
            account.remainMoney = account.remainMoney - drawMone;

            System.out.println(Thread.currentThread().getName());
            System.out.println(account.remainMoney);
        /*
        输出结果正确
         */
        }
    }
}
package ksdxc.d21;

import java.util.concurrent.CopyOnWriteArrayList;

public class d21 {
     
    public static void main(String[] args) {
     
        /*
        JUC安全类型的集合
         */
        CopyOnWriteArrayList<String> l = new CopyOnWriteArrayList<>();
        for (int i = 0; i < 99; i++) {
     
            new Thread(() -> {
     
                l.add(Thread.currentThread().getName());
            }).start();
        }
        try {
     
            Thread.sleep(120);
        } catch (InterruptedException e) {
     
            e.printStackTrace();
        }
        System.out.println(l.size());
    }
}

线程死锁问题:

  • 从执行结果可以看出,线程T1和T2歌锁住了一个资源,然后再想获取另一个线程手中的资源时,僵持住了
package ksdxc.d22;

public class d22 {
     

    public static void main(String[] args) {
     
        Thread t1 = new Thread(new MyThread5(0));
        Thread t2 = new Thread(new MyThread5(1));
        t1.start();
        t2.start();
    }

}

class MyThread5 implements Runnable {
     
    private static Object o1 = new Object();
    private static Object o2 = new Object();
    private int flag;

    public MyThread5(int flag) {
     
        this.flag = flag;
    }

    @Override
    public void run() {
     
        if (flag == 0) {
     
            synchronized (o1) {
     
                System.out.println(Thread.currentThread().getName() + "lock o1");
                synchronized (o2) {
     
                    System.out.println(Thread.currentThread().getName() + "end");
                }
            }
        } else {
     
            synchronized (o2) {
     
                System.out.println(Thread.currentThread().getName() + "lock o2");
                synchronized (o1) {
     
                    System.out.println(Thread.currentThread().getName() + "end");
                }
            }
        }
    }
}
/*
输出:
Thread-1lock o2
Thread-0lock o1
 */
    /*
    解决办法:
    拿到其中一个锁,
    使用完后就释放
    */
    @Override
    public void run() {
     
        if (flag == 0) {
     
            synchronized (o1) {
     
                System.out.println(Thread.currentThread().getName() + "lock o1");
            }
            synchronized (o2) {
     
                System.out.println(Thread.currentThread().getName() + "end");
            }
        } else {
     
            synchronized (o2) {
     
                System.out.println(Thread.currentThread().getName() + "lock o2");
            }
            synchronized (o1) {
     
                System.out.println(Thread.currentThread().getName() + "end");
            }
        }
    }

你可能感兴趣的:(2021.09,java,数据库)