目录
一、锁
1. synchronized处理多线程并发编程。
重量级锁。1.6对synchronized进行了优化。
1.6为了减少获得锁和释放锁带来的性能消耗引入的偏向锁和轻量级锁
2. synchronized有三种方式来加锁,分别是:
1.修饰实例方式,作用于当前实例加锁,进入同步代码前要获得当前实例的锁
2.静态方法,作用于对当前类对象加锁,进入同步代码前要获得当前类对象的锁
3.代码块,指定加锁对象,给指定的对象加锁,进入同步代码块之前要获得给定对象的锁
1.实例方法:调用该方法的实例
2.静态方法:类对象
3.this:调用该方法的实例对象
4.类对象:类对象
操作共享数据的代码
共享数据:多个线程共同操作的变量,可以充当锁
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解决问题!!!
public class Window implements Runnable {
private String name;
private static int ticket = 10;
private static Object object = new Object();
public Window(String name) {
this.name = name;
}
@Override
public void run() {
synchronized (Window.class) {//(Object)或者是类对象*推荐
while (true) {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name + "窗口,剩余" + ticket-- + "张");
} else {
break;
}
}
}
}
}
class Ch02{
public static void main(String[] args) {
Thread t1=new Thread(new Window("1"));
Thread t2=new Thread(new Window("2"));
t1.start();
t2.start();
}
}
3.死锁
死锁是这样一个情形:多线程同时被阻塞,他们中的一个或者全部
都在等待某个资源的释放,由于线程无限期的阻塞,程序就不可能正常终止死锁产生四个必要条件:
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开始执行");
while (true){
synchronized (Ch03.obj1){
System.out.println(new Date().toString()+"LockA锁住了obj1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized (Ch03.obj2){
System.out.println(new Date().toString()+"LockA锁住了obj2");
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class LockB implements Runnable{
@Override
public void run() {
System.out.println(new Date().toString()+"LockB开始执行");
while (true){
synchronized (Ch03.obj1){
System.out.println(new Date().toString()+"LockB锁住了obj1");
try {
Thread.sleep(6*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized (Ch03.obj2){
System.out.println(new Date().toString()+"LockB锁住了obj2");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public class Ch03 {
public static String obj1="obj1";
public static String obj2="obj2";
public static void main(String[] args) {
LockA lockA=new LockA();
new Thread(lockA).start();
LockB lockB=new LockB();
new Thread(lockB).start();
}
4. 线程重入
任意的线程在拿到锁之后,再次获取该锁不会被该锁阻碍
线程不会被自己锁死
这就叫线程重入,synchronized可重入锁JDK1.6升级锁
1.无锁:不加锁
2.偏向锁:不锁锁,只有一个线程争夺时,偏向某一个线程,这个线程不加锁
3.轻量级锁:少量线程来了之后,想尝试自旋,不挂起线程。
4.重量级锁:排队挂起线程。(synchronized)挂起线程和恢复线程需要转入内核态中完成这些操作,给系统的并发性带来很大的压力。
在许多应用上共享数据的锁定状态,只会持续很短的时间,为了这段时间去挂起和恢复并不值得
我们可以让后面的线程等待一下,不要放弃处理器的执行时间。
锁为了让线程等待,我们只需要让线程执行一个循环,自旋。【自旋锁】hashcode 值 1 0
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();
}
5. Object类对多线程的支持
wait():
wait(long timeout):当前线程进入等待状态
notify():唤醒正在等待的下一个线程
notifyAll():唤醒正在等待的所有线程*线程间的通信
*比如两条线程,共同运行。
*线程A如果先走,线程就要等待。等待线程A走完,唤醒线程B,线程B再走
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(1000);//等待
} 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();
}
6. 方法总结:
1.Thread的两个静态方法:
sleep释放CPU资源,但是不会释放锁
yield方法释放CPU的执行权,保留CPU的执行资格,不常用2.join方法,yield出让了执行权,join就加入了进来
3.wait:释放CPU资源,释放锁
notify:唤醒等待线程
notifyAll:唤醒等待的所有线程
面试题:sleep和wait的区别?
1.出处:sleep来自于Thread类,wait来自Object类
2.锁的控制: sleep释放CPU资源,但是不会释放锁;wait:释放CPU资源,释放锁
7. 线程的退出
使用退出标志,线程正常退出,run方法结束后线程终止
class MyThread extends Thread{
boolean flag=true;
@Override
public void run() {
while (flag){
try {
System.out.println("线程一直运行");
int i=10/0;
}catch (Exception e){
System.out.println("线程停止运行");
this.stopThread();
}
}
}
private void stopThread() {
System.out.println("线程一直运行");
this.flag=false;
}
}
public class Ch03 {
public static void main(String[] args) {
MyThread myThread=new MyThread();
myThread.start();
}
}
8. 中断线程
interrupt方法
调用interrupt方法会抛出InterruptedException异常,
捕获后再做停止线程的逻辑即可。
如果线程while(true)运行的状态,interrupt方法无法中断线程
class MyThread2 extends Thread{
volatile boolean flag;
public void run(){
while (flag){
synchronized (this){
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
this.stopThread();
}
}
}
}
private void stopThread() {
System.out.println("");
this.flag=false;
}
}
public class Ch04 {
public static void main(String[] args) {
MyThread2 myThread2 = new MyThread2();myThread2.start();
System.out.println("线程开始...");
try {
Thread.sleep(500);
} catch (InterruptedException e){
e.printStackTrace();
}
//中断线程的执行
myThread2.interrupt();
}
}
9. 线程的常用方法:
Thread类中的方法
1.start:启动线程,执行run方法
2.run:
3.currentThread:静态方法,获取当前正在执行的线程
4.getID():返回此线程的唯一标识
5.setName(String):设置当前线程name
6.getName():设置当前线程name
7.getPriority():获取当前线程的优先级
8.setPriority():设置当前线程的优先级
9.getState():中断线程执行
10.interrupt():测试当前线程是否中断
11.isDaemon();测试当前线程是否是守护线程
10.懒汉式:过时 内部类,枚举
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) {
}
}
二、小案例
生产者消费者案例中包含的类:
奶箱类(Box):定义一个成员变量,表示第x瓶奶,提供存储牛奶和获取牛奶的操作
生产者方法(Producer):synchronized void put(int milk)
//消费者方法
synchronized void get(){
测试类(BoxDemo):里面有main方法,main方法中的代码步骤如下(采用箭头函数)
1.创建2个线程对象,生产者对象和消费者对象
启动线程
方法一:
public class Box {
int x;
static boolean flag=false;
synchronized void put(int milk){
if (flag==true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.x=milk;
System.out.println(Thread.currentThread().getName()+"---生产"+x);
flag=true;
notify();
}
synchronized void get(int i){
if (!flag){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"---消费"+x);
flag = false;
notify();
}
}
class BoxDemo{
public static void main(String[] args) {
Box box=new Box();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <=10 ; i++) {
box.put(i);
}
}
},"生产者").start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 1; i <=10 ; i++) {
box.get(i);
}
}
},"消费者").start();
}
}
方法二:
public class MilkBox {
boolean flag=false;
public synchronized void put(){
if (flag==true){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("开始生产牛奶");
System.out.println("生产完成");
flag=true;
notify();
}
public synchronized void get(){
if (!flag){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("正在喝牛奶");
System.out.println("牛奶喝完了");
System.out.println("-----------");
flag=false;
notify();
}
}
class MilkDemo{
public static void main(String[] args) {
MilkBox milkBox=new MilkBox();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
milkBox.put();
}
}
},"生产者").start();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
milkBox.get();
}
}
},"消费者").start();
}
}
三、心得体会
被线程折磨的一天,锁懵了!