通过尚硅谷的视频学习java基础,记录每日所学,温故而知新,本文通过三个线程的同步互斥机制解决多窗口售票问题
一、继承Thread类的两种同步方法
继承的方式需要注意的是:创建的三个线程对应了三个Thread实现类的对象,所以同步使用的对象必须是静态的,一个静态属性/方法对应一个类,在类加载时创建,不受创建对象的影响
1、同步代码块
同步使用synchronized(任意唯一对象) ,关键在于多个线程使用唯一相同的对象,ThreadTest.class 可以用静态的object 替换
public class ThreadTest extends Thread {
private static int num = 100;
//private static Object object = new Object();
@Override
public void run() {
while (true) {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (ThreadTest.class) { //同步代码块
if (num > 0) {
System.out.println(getName() + ":" + num);
--num;
} else {
break;
}
}
}
}
public static void main(String[] args) {
ThreadTest threadTest1 = new ThreadTest();
ThreadTest threadTest2 = new ThreadTest();
ThreadTest threadTest3 = new ThreadTest();
threadTest1.start();
threadTest2.start();
threadTest3.start();
}
}
执行结果
2、同步方法
在方法前加上static synchronized
public class ThreadTest extends Thread {
private static int num = 100;
//private static Object object = new Object();
@Override
public void run() {
while (num > 0) {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
show();
}
}
private static synchronized void show() {
if (num > 0) {
System.out.println(Thread.currentThread().getName() + ":" + num);
--num;
}
}
public static void main(String[] args) {
ThreadTest threadTest1 = new ThreadTest();
ThreadTest threadTest2 = new ThreadTest();
ThreadTest threadTest3 = new ThreadTest();
threadTest1.start();
threadTest2.start();
threadTest3.start();
}
}
注意: while(num > 0) ;
private static synchronized void show() 方法默认的对象是Thread.class
二、实现Runnable 接口的方式
此类方式只创建了一个Runnable 实现类的对象,所以同步时的对象可以是自定义的实现类里面的任一属性(无静态要求),或者是this
1、同步代码块
public class ThreadTest2 implements Runnable {
private int num = 100;
//Object obj = new Object();
@Override
public void run() {
while (true) {
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (this) {
if (num > 0) {
System.out.println(Thread.currentThread().getName() + ":" + num);
num--;
} else {
break;
}
}
}
}
public static void main(String[] args) {
ThreadTest2 threadTest2 = new ThreadTest2();
Thread thread = new Thread(threadTest2);
thread.start();
Thread thread2 = new Thread(threadTest2);
thread2.start();
Thread thread3 = new Thread(threadTest2);
thread3.start();
}
}
执行结果:
2、同步方法
public class ThreadTest2 implements Runnable {
private int num = 1000;
//Object obj = new Object();
@Override
public void run() {
while (num > 0) {
show();
}
}
private synchronized void show() {
if (num > 0) {
System.out.println(Thread.currentThread().getName() + ":" + num);
num--;
}
}
public static void main(String[] args) {
ThreadTest2 threadTest2 = new ThreadTest2();
Thread thread = new Thread(threadTest2);
Thread thread2 = new Thread(threadTest2);
Thread thread3 = new Thread(threadTest2);
thread.start();
thread2.start();
thread3.start();
}
}
三、懒汉式单例模式的优化
public class ThreadLan {
private ThreadLan() {
}
private static ThreadLan instance;
public static ThreadLan getInstance() {
if (instance == null) { //优化
synchronized (ThreadLan.class) {
if (instance == null) {
instance = new ThreadLan();
}
}
}
return instance;
}
}
四、多线程死锁举例
产生死锁是因为各线程互相持有一定资源并等待对方释放其所需的资源,在多线程访问共享资源时,对于同步的代码,主要体现在锁上,而非共享代码上
public class ThreadWo {
static StringBuffer s1 = new StringBuffer();
static StringBuffer s2 = new StringBuffer();
public static void main(String[] args) {
new Thread() {
@Override
public void run() {
synchronized (s1) {
s1.append("a");
s2.append("1");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s2) {
s1.append("b");
s2.append("2");
System.out.println(s1);
System.out.println(s2);
}
}
}
}.start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (s2) {
s2.append("3");
s1.append("c");
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (s1) {
s2.append("4");
s1.append("d");
System.out.println(s1);
System.out.println(s2);
}
}
}
}).start();
}
}
产生死锁是因为线程1掌握着资源s1,申请者s2,而线程2掌握着s2,申请者s1,执行结果不会报错,会一直卡着
注意: 如果某资源不能申请使用,那就是不能被访问,对资源的任何操作都不行,这也就是同步代码时为什么需要锁对象的唯一,这样才能阻塞其他线程
五、解决线程安全的另一方法lock锁
ReentrantLock 类实现了Lock 接口,创建ReentrantLock 对象,调用lock() 和 unlock() 方法实现上锁和解锁
public class LockTest implements Runnable {
private int num = 100;
private ReentrantLock reent = new ReentrantLock();
@Override
public void run() {
while (num > 0) {
try {
reent.lock(); //上锁
if (num > 0) {
System.out.println(Thread.currentThread().getName() + ":" + num);
num--;
}
} finally {
reent.unlock(); //解锁
}
}
}
public static void main(String[] args) {
LockTest lockTest = new LockTest();
Thread thread1 = new Thread(lockTest);
Thread thread2 = new Thread(lockTest);
Thread thread3 = new Thread(lockTest);
thread1.start();
thread2.start();
thread3.start();
}
}
六、Lock 与synchronized 解决线程安全问题的区别
1、synchronized 同步代码后会自动释放同步监视器,Lock 需要手动释放
2、建议使用优先级:Lock > synchronized 同步代码块 > synchronized 同步方法