学习笔记——LOCK
一、学习重点
二、学习内容
案例一:
生产者与消费者模型
两条线程,一条线程生产产品,另一条线程消费产品
思路:
这两天线程,初始状态是什么情况?
电脑工厂,生产电脑是需要时间,生产完毕
唤醒这些等待的消费者。等待。唤醒工厂,继续生产
消费者,等待,被唤醒,电脑都卖出去了,等待
class Factory implements Runnable { @Override public void run() { synchronized (Ch02.OBJ){ while(true){ // 生产电脑 System.out.println("工厂生产电脑,已经生产了:" + Ch02.count ++ + "台!"); if(Ch02.count >= 100) { notifyAll(); try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } class Consumer implements Runnable { @Override public void run() { synchronized (Ch02.OBJ){ while(true){ if(Ch02.count >= 0){ // 消费电脑 System.out.println("消费者消费了1台电脑,剩余:" + Ch02.count-- + "台!"); } if(Ch02.count <= 0){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } notifyAll(); } } } } } public class Ch02 { public static final Object OBJ = new Object(); public static int count = 0; public static void main(String[] args) { Thread t1 = new Thread(new Factory()); Thread t2 = new Thread(new Consumer()); t1.start(); t2.start(); } }
案例二:生产者与消费者模型(构造器)
public class Car { private String name; private Integer id; private boolean flag = false; public Car() { } public Car(String name, Integer id) { this.name = name; this.id = id; } // 买车 public synchronized void get() { if(!flag) { // 要等待汽车的生产 try { super.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("购买了汽车" + this.name + "->" + this.id); flag = false; super.notify(); } // 产车 public synchronized void set(String name,Integer id){ if(flag) { try { super.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.name = name; try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } this.id = id; System.out.println("生产了汽车:" + name + "->" + id); flag = true; super.notify(); } } //***************************************************************************************************** public class CarConsumer implements Runnable { private Car car; public CarConsumer() { } public CarConsumer(Car car) { this.car = car; } @Override public synchronized void run() { for (int i = 1; i <= 100; i++) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } car.get(); } } } //******************************************************************************************************* /** * Callable接口:抛异常 * run方法,不能抛异常 * * 生产者 */ public class CarProduction implements Runnable { private Car car; public CarProduction() { } public CarProduction(Car car) { this.car = car; } @Override public synchronized void run() { for (int i = 1; i <= 100 ; i++) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } if(i % 2 == 0){ car.set("汽车" + i,i); }else { car.set("卡车" + i,i); } } } } //******************************************************************************************************* public class Demo { public static void main(String[] args) { Car car = new Car(); CarProduction carProduction = new CarProduction(car); CarConsumer carConsumer = new CarConsumer(car); new Thread(carConsumer).start(); new Thread(carProduction).start(); } }
案例三:生产者与消费者模型(内部类)
class Product{ int n;//共享数据(数据仓库) boolean flag=false;//true 代表 n 有值,false 代表没有值 //生产方法 synchronized void put(int n){ if(flag==true){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.n=n; System.out.println(Thread.currentThread().getName() +"--生产"+n); //生产完毕通知消费者,进行取值操作 flag=true; notify(); } //消费 synchronized void get(){ if(!flag){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //取值 System.out.println(Thread.currentThread().getName()+ "--消费"+n); //消费完毕,没值了,通知生产者生产线程, flag=false; notify(); } } public class ProductXiaoFeiTest { public static void main(String[] args) { Product p=new Product(); //生产者线程 new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <=10; i++) { p.put(i); } } },"生产者").start(); //消费者线程 new Thread(new Runnable() { @Override public void run() { for (int i = 1; i <=10; i++) { p.get(); } } },"消费者").start(); } }
//具体实例
class BaoZi{ boolean flag=false;//包子是否存在 true有包子 消费 flase 没包子 生产 String pier; String xian; public BaoZi() { } public BaoZi(String pier, String xian) { this.pier = pier; this.xian = xian; } //生产包子 public synchronized void productBaoZi(){ if(flag==true){//有包子 try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } //生产 System.out.println("包子铺开始做包子"); System.out.println("包子作好了"+this.pier+this.xian); flag=true; notify();//通知对方开始吃包子 } //消费包子 public synchronized void eatBaozi(){ if(!flag){//包子没作做好 try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("吃货正在吃包子"+this.pier+this.xian); System.out.println("===================="); flag=false; notify();//通知对方做包子 } } public class BaoZiPuTest { public static void main(String[] args) { BaoZi bz = new BaoZi("薄皮", "牛肉"); //生产线程 new Thread(new Runnable() { @Override public void run() { while (true) { //生产包子需要时间 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } bz.productBaoZi(); } } }).start(); //消费线程 new Thread(new Runnable() { @Override public void run() { while (true) { bz.eatBaozi(); } } }).start(); } }
三、笔记内容
锁
1、synchronized处理多线程并发编程。(重量级锁)
JDK1.6对synchronized进行了优化
JDK1.6为了减少获得锁和释放锁带来的性能消耗引入的偏向锁和轻量级锁
synchronized有三种方式来加锁,分别是:
1、修饰实例方法,作用于当前实例加锁,进入同步代码前要获得当前实例的锁
2、静态方法,作用于当前类对象加锁,进入同步代码前要获得的当前类对象的锁
3、同步代码块,指定加锁对象,对给定对象加锁,进入同步代码块之前,要获得给定对象的锁
1、实例方法:调用该方法的实例
2、静态方法:类对象 (Ch01.class;)
3、this:调用该方法的实例对象
4、类对象:类对象
操作共享数据的代码
共享数据:多个线程共同操作的变量,都可以充当锁
public class Ch01 { public static void main(String[] args) { //同步代码块 //创建一个对象 //类对象 //当前实例this //同步监视器 synchronized (Ch01.class){ int a = 1; } } }
当使用同步方法的时,synchronized锁的东西是this
关于同步方法
1、同步方法依然会涉及到同步锁对象,不需要写出来
2、非静态的同步方法,同步锁就是this
静态的同步方法,同步监视器就是类本身
同步代码块:
1、选好同步监视器(锁)推荐使用类对象,第三方对象,this
2、在实现接口创建的线程类中同步代码块不可以用this来充当同步锁
同步的方式,解决线程安全的问题。
操作同步代码时,只有一个线程能够参与,其它线程等待。
相当于一个单线程的过程,效率低
synchronized只针对当前JVM可以解决线程安全问题
synchronized不可以跨JVM解决问题!!!
class Window implements Runnable { private static int ticket = 100; private String name; private static Object obj = new Object(); public Window(String name) { this.name = name; } @Override public void run() { // sell(); synchronized (Window.class) { while(true) { if(ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(name + "卖票,剩余:" + ticket-- + "张!"); }else { break; } } } } private synchronized void sell() { while(true) { if(ticket > 0) { try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(name + "卖票,剩余:" + ticket-- + "张!"); }else { break; } } } } public class Ch02 { public static void main(String[] args) { // Window w1 = new Window("窗口一"); // Window w2 = new Window("窗口二"); Thread t1 = new Thread(new Window("窗口一")); Thread t2 = new Thread(new Window("窗口二")); // Thread t1 = new Thread(w); // Thread t2 = new Thread(w); t1.start(); t2.start(); } }
死锁
多个线程同时被阻塞,它们中的一个或者全部
都在等待某个资源的释放,由于线程的无限期阻塞,程序就不可能正常终止
Java死锁产生四个必要条件
1、互斥使用了,当资源被一个线程使用(占用),别的线程不能使用
2、不可抢占,资源的请求者不能强制从占有者中抢夺资源,资源只能从占有者手动释放
3、请求和保持。
4、循环等待,存在一个等待队列。P1占有P2资源,P2占有P3资源,P3占有P1的资源。形成了一个等待环路
class LockA implements Runnable{ @Override public void run() { System.out.println(new Date().toString() + "LockA开始执行..."); try { while (true) { synchronized (Ch04.obj1) { System.out.println(new Date().toString() + "LockA锁住了obj1"); Thread.sleep(8000); } synchronized (Ch04.obj2) { System.out.println(new Date().toString() + "LockA锁住了obj2"); Thread.sleep(70*1000); } } } catch (InterruptedException e) { e.printStackTrace(); } } } class LockB implements Runnable { @Override public void run() { System.out.println(new Date().toString() + "LockB开始执行..."); try { while (true) { synchronized (Ch04.obj2) { System.out.println(new Date().toString() + "LockB锁住了obj2"); Thread.sleep(6000); //3s } synchronized (Ch04.obj1) { System.out.println(new Date().toString() + "LockB锁住了obj1"); Thread.sleep(100*1000); // 1min } } } catch (InterruptedException e) { e.printStackTrace(); } } } public class Ch04 { static String obj1 = "obj1"; static String obj2 = "obj1"; public static void main(String[] args) { LockA lockA = new LockA(); new Thread(lockA).start(); LockB lockB = new LockB(); new Thread(lockB).start(); } }
线程重入
任意线程在拿到锁之后,再次获取该锁不会被阻碍
线程不会被自己锁死的
这就叫线程的重入,synchronized可重入锁
JDK1.6以后锁升级
1、无锁:不加锁
2、偏向锁:不锁锁,只有一个线程争夺时,偏向某一个线程,这个线程不加锁
3、轻量级锁:少量线程来了之后,先尝试自旋,不挂起线程
4、重量级锁:排队挂起(暂停)线程 synchronized
挂起线程和恢复线程需要转入内核态中完成这些操作,给系统并发性带来很大的压力
在许多应用上共享数据的锁定状态,只会持续很短的时间,为了这段时间去挂起和恢复并不值得
我们可以让后面的线程等待一下,不要放弃处理器的执行时间。锁定为了让线程等待,我们只需要让线程
执行一个时间。【自旋锁】
hashcode值1 0
public class Ch05 { private static final Object M1 = new Object(); private static final Object M2 = new Object(); public static void main(String[] args) { new Thread(() -> { synchronized (M1) { synchronized (M2) { synchronized (M1) { synchronized (M2){ System.out.println("hello lock"); } } } } }).start(); } }
Object类对多线程的支持
wait():当前线程进入等待状态
wait(long timeout):当前线程进入等待状态
notify():唤醒正在等待的下一个线程
notifyAll():唤醒正在等待的所有线程
线程间的通信
比如两条线程,共同运行。
线程A先走,线程B等待,线程A走完,唤醒线程B,线程B再走
public class Ch01 { private static int num = 10; private static final Object OBJ = new Object(); public static void plus(int code,int i) { synchronized (OBJ) { if(num >= 10){ // 唤醒其他等待的线程 // OBJ.notify(); // 唤醒所有等待的线程 OBJ.notifyAll(); } System.out.println("这是线程" + code + "->" + ++num + "->" + i); } } public static void sub(int code,int i) { synchronized (OBJ) { if(num <= 0){ try { // 减法上来就是等待的状态 OBJ.wait(200); } catch (InterruptedException e) { throw new RuntimeException(e); } } System.out.println("这是线程" + code + "->" + --num + "->" + i); } } public static void main(String[] args) { Thread t1 = new Thread(() -> { for (int i = 0;; i++) { try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } sub(1,i); } }); Thread t2 = new Thread(() -> { for (int i = 0;; i++) { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } plus(2,i); } }); t1.start(); t2.start(); System.out.println("--------------------------------------------------"); } }
方法总结案例一
1、Thread的两个静态方法:
sleep释放CPU资源,但是不会释放锁(sleep开发中尽量不写)
yield方法释放CPU的执行权,保留CPU的执行资格,不常用。
2、join方法,yield出让执行权,join就加入进来。
3、wait:释放CPU资源,释放锁
notify():唤醒正在等待的下一个线程
notifyAll():唤醒正在等待的所有线程
面试题:sleep和wait的区别?
1、出处
2、锁的控制
线程的退出
使用退出标志,线程正常退出,Run方法结束后线程终止
不要使用stop方法
system。exit(-1)
class MyThread extends Thread { volatile boolean flag = true; @Override public void run() { while(flag) { try { System.out.println("线程一直在运行..."); int i = 10 / 0; } catch (Exception e) { this.stopThread(); } } } public void stopThread() { System.out.println("线程停止运行..."); this.flag = false; } } public class Ch03 { public static void main(String[] args) { MyThread myThread = new MyThread(); myThread.start(); } }
interrupt方法
中断线程
调用interrupt方法会抛出InterruptedException异常
捕获后再做停止线程的逻辑即可
如果线程while(true)运行的状态,interrupt方法无法中断线程。
class MyThread02 extends Thread { private boolean flag = true; @Override public void run() { while(flag) { synchronized (this){ // try { // wait(); // } catch (InterruptedException e) { // e.printStackTrace(); // } try { sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); this.stopThread(); } } } } public void stopThread() { System.out.println("线程停止运行..."); this.flag = false; } } public class Ch04 { public static void main(String[] args) { MyThread02 myThread02 = new MyThread02(); myThread02.start(); System.out.println("线程开始..."); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } // 中断线程的执行 myThread02.interrupt(); } }
线程的常用方法:
Thread类中的方法
1、start:启动当前线程;执行R、run方法
2、run:
3、currentThread:静态方法,获取当前正在执行的线程
4、getId():返回此线程的唯一标识
5、setName():设置当前线程的name
6、getName():获取当前线程的name
7、getPriority():获取当前线程的优先级
8、setPriority(int):设置当前线程的优先级
9.getState():获取当前线程的声明周期
10.interrupt():中断线程的执行
11.interrupted():查看当前线程是否中断
class MyThread03 extends Thread { @Override public void run() { System.out.println(getId()); setName("线程一"); System.out.println(getName()); } } public class Ch05 { public static void main(String[] args) { // Thread thread = Thread.currentThread(); // System.out.println(thread); // 如何获取主线程的优先级 // Thread.currentThread().setPriority(Thread.MIN_PRIORITY); MyThread03 myThread03 = new MyThread03(); // myThread03.setPriority(Thread.MAX_PRIORITY); myThread03.setDaemon(true); myThread03.start(); // System.out.println(myThread03.getState()); System.out.println(myThread03.isDaemon()); // System.out.println("主" + Thread.currentThread().getPriority()); // System.out.println("自己的:" + myThread03.getPriority()); // new MyThread03().start(); // new MyThread03().start(); } }
懒汉式:过时
内部类,枚举(天生构造器私有化)
class Singleton { private static Singleton instant; private Singleton(){} public static Singleton getInstance() { if(instant == null) { // 卡了 synchronized (Singleton.class) { if(instant == null){ instant = new Singleton(); } } } return instant; } } public class Ch06 { public static void main(String[] args) { System.out.println(Singleton.getInstance() == Singleton.getInstance()); } }