它是多线程并发编程,重量级锁。JDK1.6对synchronized进行了优化。
JDK1.6为了减少获得锁和释放锁带来的性能消耗引入的偏向锁和轻量级锁。
synchronized有三种方式来加锁:
- 修饰实例方法,作用于当前实例加锁,进入同步代码前要获得当前实例的锁
- 静态方法,作用于当前类对象加锁,进入同步代码前要获得的当前类对象的锁
- 代码块,指定加锁对象,对给定对象加锁,进入同步代码块之前要获得给定对象的锁
synchronized都可以给哪些上锁
- 实例方法:调用该方法的实例
- 静态方法:类对象
- this:调用该方法的实例对象
- 类对象:类对象
基础运用
操作共享数据的代码
共享数据:多个线程共同操作的变量,都可以充当锁
public class Ch01 { public static void main(String[] args) { // 同步代码块 // 创建一个对象 // 类对象 // 当前实例this // 同步监视器 synchronized (Ch01.class) { int a = 1; } } }
当使用同步方法时,synchronize锁的东西是this
关于同步方法:- 同步方法依然会涉及到同步锁对象,不需要我们写出来
- 非静态的同步方法,同步锁就是this;静态的同步方法,同步监视器就是类本身
- 选好同步监视器(锁)推荐使用类对象,第三方对象,this
- 在实现接口创建的线程类中,同步代码块不可以用this来充当同步锁
操作同步代码时,只有一个线程能够参与,其他线程等待
相当于一个单线程的过程,效率低synchronized只针对于当前JVM可以解决线程安全问题。
synchronized不可以跨JVM解决问题!!!
加锁卖票
class Windows3 extends Thread { private static int tickets = 100; private String name; public Windows3(String name) { this.name = name; } @Override public void run() { sell(); } private synchronized void sell() { while (true) { if (tickets >0 ) { try { Thread.sleep(20); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(name+"卖票,剩余:"+tickets-- +"张!"); } else { break; } } } } // 卖票 public class Ch03 { public static void main(String[] args) { Windows3 w1 = new Windows3("one"); Windows3 w2 = new Windows3("two"); w1.start(); w2.start(); } }
死锁
死锁的情形:多个线程同时被阻塞,他们中的一个或者全部
都在等待某个资源的释放,由于线程无限期的阻塞,程序就不可能正常终止
Java死锁产生有四个必要条件
- 互斥使用,当资源被一个线程使用(占用),别的线程不能使用。
- 不可抢占,资源请求者不能强制从占有者中抢夺资源,资源只能从占有者手动释放
- 请求和保持
- 循环等待,存在一个等待队列,P1占有P2的资源,P2占有了P3的资源,P3占有P1的资源,形成了一个等待环路
线程重入
任意线程在拿到锁之后,再次获取该锁不会被该锁所阻碍
线程不会被自己锁死的。这就叫线程的重入,synchronized可重入锁。
- 无锁:不加锁
- 偏向锁:不锁锁,只有一个线程争夺时,偏向某一个线程,这个线程不加锁
- 轻量级锁:少量线程来了之后,向尝试自旋,不挂起线程。
- 重量级锁:排队挂起(暂停)线程。(synchronized)
挂起线程和恢复线程需要转入内核态中完成这些操作,给系统的并发性带来很大的压力。
在许多应用上共享数据的锁定状态,只会持续很短的时间,为了这段时间去挂起和恢复并不值得
我们可以让后面的线程等待一下,不要放弃处理器的执行时间。锁为了让线程等待,我们只需要让线程执行一个循环,自旋。【自旋锁】
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():当前线程进入等待状态
notify():唤醒正在等待的下一个线程
notifyAll():唤醒正在等待的所有线程
比如两条线程,共同运行。
线程A如果先走,线程B就要等待。等待线程A走完,唤醒线程B,线程B再走
- Thread的两个静态方法:
sleep释放CPU资源,但是不会释放锁
yield方法释放CPU执行权,保留了CPU的执行资格,不常用。 - join方法,yield出让了执行权,join就加入进来。
- wait:释放CPU资源,释放锁
notify:唤醒等待中的线程
notifyAll:唤醒等待中的所有线程
- 使用 sleep 方法可以让线程休眠,而使用 wait 方法则必须放在 synchronized 块里面。
- wait还需要额外的方法notify/ notifyAll 进行唤醒,它们同样需要放在 synchronized 块里面,且获取对象的锁。当然也可以使用带时间的 wait(long millis) 方法,时间一到,无需其他线程唤醒,也会重新竞争获取对象的锁继续执行。
- sleep方法短暂休眠之后会主动退出阻塞,而没有指定时间的wait方法则需要被其他线程中断后才能退出阻塞。sleep()和yield()是Thread类的方法。
- wait() 是Object中定义的native方法。sleep()方法自带sleep时间,时间过后,Thread会自动被唤醒。 或者可以通过调用interrupt()方法来中断。
案例:生产者与消费者模型
两条线程,一条线程生产产品,另一条线程消费产品
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(); } }
线程的退出
使用退出标志,线程正常退出,run方法结束后线程终止,不要使用stop方法。
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 { 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类中的方法
- start:启动当前线程;执行run方法
- run:Thread中需要重写的方法
- currentThread:静态方法,获取当前正在执行的线程
- getId():返回此线程的唯一标识
- setName(String):设置当前线程的name
- getName():获取当前线程的name
- getPriority():获取当前线程的优先级
- setPriority(int):设置当前线程的优先级
- getState():获取当前线程的声明周期
- interrupt():中断线程的执行
- 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) { MyThread03 myThread03 = new MyThread03(); myThread03.setDaemon(true); myThread03.start(); System.out.println(myThread03.isDaemon()); } }
多线程的单例
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()); } }
经典例题 包子
全部代码