
package com.example.demo;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author chenxf
* @create 2021-08-03 23:09
* 多线程顺序调用 A->B->C
* AA打印5次 BB打印10次 CC打印15次 循环10轮
*/
class ShareResource{
private int number = 1; // A:1 B:2 C:3
private Lock lock = new ReentrantLock();
private Condition c1 = lock.newCondition();
private Condition c2 = lock.newCondition();
private Condition c3 = lock.newCondition();
public void print5(){
lock.lock();
try{
// 1判断
while(number != 1){
c1.await();
}
// 2干活
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName()+"\t"+i);
}
// 3通知
number = 2;
c2.signal();
}catch(Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void print10(){
lock.lock();
try{
// 1判断
while(number != 2){
c2.await();
}
// 2干活
for (int i = 1; i <= 10; i++) {
System.out.println(Thread.currentThread().getName()+"\t"+i);
}
// 3通知
number = 3;
c3.signal();
}catch(Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
public void print15(){
lock.lock();
try{
// 1判断
while(number != 3){
c3.await();
}
// 2干活
for (int i = 1; i <= 15; i++) {
System.out.println(Thread.currentThread().getName()+"\t"+i);
}
// 3通知
number = 1;
c1.signal();
}catch(Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}
}
}
public class SyncAndReentrantLockDemo {
public static void main(String[] args) {
ShareResource shareResource = new ShareResource();
new Thread(() -> {
for (int i = 1; i <= 10 ; i++) {
shareResource.print5();
}
},"A").start();
new Thread(() -> {
for (int i = 1; i <= 10 ; i++) {
shareResource.print10();
}
},"B").start();
new Thread(() -> {
for (int i = 1; i <= 10 ; i++) {
shareResource.print15();
}
},"C").start();
}
}
synchronized什么时候是对象锁?什么时候是全局锁?
synchronized(this)以及非static的synchronized方法,锁住的是括号里()的对象,如果要锁全局,就用synchronized(对象.class)。
static 的synchronized方法,static方法可以直接类名.方法名()调用,无法使用this,所以它锁的不是this,而是类的Class对象。所以静态方法也相当于全局锁,锁住了代码段。
具体看下面的代码
对象锁和全局锁区别:
对象锁:锁的是这个对象。
全局锁:锁的是那个写了synchronized关键字的方法或者代码块。
举个例子:假设A类创建了a、b、c三个对象,然后a使用对象锁住test()方法,b和c依然可以访问test()方法,对象锁只能防止别的线程调用a对象的test方法。而如果a使用全局锁,锁住的是真正的代码块,b和c就不能使用test方法。
synchronized定义对象锁:
1、在方法内部加synchronized关键字定义:
public String sellTicket2(){
// 在方法内部加synchronized代码块
synchronized (this){ // 表示锁住对象
// 要锁住的代码
}
return "";
}
2、在方法头用synchronized关键字修饰:
public synchronized String sellTicket3(){
// 要锁的代码
return "";
}
synchronized定义全局锁:
// 真正锁住代码
public String sellTicket4(){
synchronized (Main.class){
// 要锁的代码
}
return "";
}