synchronized
锁java对象,锁class对象,锁代码块
ReentrantLock 悲观锁,可重入锁,公平锁,非公平锁
package com.example.myapplication.demo;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockDemo {
static class ReentrantLockTask{
ReentrantLock reentrantLock = new ReentrantLock();
void buyTicket(){
String name= Thread.currentThread().getName();
try {
reentrantLock.lock();
System.out.println(name+":准备好了");
Thread.sleep(100);
System.out.println(name+":买好了");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
reentrantLock.unlock();
}
}
}
public static void main(String[] args) {
final ReentrantLockTask task = new ReentrantLockTask();
Runnable runnable = new Runnable() {
@Override
public void run() {
task.buyTicket();
}
};
for (int i = 0; i < 10; i++) {
new Thread(runnable).start();
}
}
}
//执行结果:
/*
Thread-2:准备好了
Thread-2:买好了
Thread-3:准备好了
Thread-3:买好了
Thread-4:准备好了
Thread-4:买好了
Thread-5:准备好了
Thread-5:买好了
Thread-6:准备好了
Thread-6:买好了
Thread-7:准备好了
Thread-7:买好了
Thread-8:准备好了
Thread-8:买好了
Thread-9:准备好了
Thread-9:买好了
Thread-10:准备好了
Thread-10:买好了
Thread-11:准备好了
Thread-11:买好了
*/
package com.example.myapplication.demo;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockDemo {
static class ReentrantLockTask{
ReentrantLock reentrantLock = new ReentrantLock();
void buyTicket(){
String name= Thread.currentThread().getName();
try {
reentrantLock.lock();
System.out.println(name+":准备好了");
Thread.sleep(100);
System.out.println(name+":买好了");
reentrantLock.lock();
System.out.println(name+":又准备好了");
Thread.sleep(100);
System.out.println(name+":又买好了");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
reentrantLock.unlock();
reentrantLock.unlock();
}
}
}
public static void main(String[] args) {
final ReentrantLockTask task = new ReentrantLockTask();
Runnable runnable = new Runnable() {
@Override
public void run() {
task.buyTicket();
}
};
for (int i = 0; i < 10; i++) {
new Thread(runnable).start();
}
}
}
/*
Thread-2:准备好了
Thread-2:买好了
Thread-2:又准备好了
Thread-2:又买好了
Thread-3:准备好了
Thread-3:买好了
Thread-3:又准备好了
Thread-3:又买好了
Thread-4:准备好了
Thread-4:买好了
Thread-4:又准备好了
Thread-4:又买好了
Thread-5:准备好了
Thread-5:买好了
Thread-5:又准备好了
Thread-5:又买好了
Thread-6:准备好了
Thread-6:买好了
Thread-6:又准备好了
Thread-6:又买好了
Thread-7:准备好了
Thread-7:买好了
Thread-7:又准备好了
Thread-7:又买好了
Thread-8:准备好了
Thread-8:买好了
Thread-8:又准备好了
Thread-8:又买好了
Thread-9:准备好了
Thread-9:买好了
Thread-9:又准备好了
Thread-9:又买好了
Thread-10:准备好了
Thread-10:买好了
Thread-10:又准备好了
Thread-10:又买好了
Thread-11:准备好了
Thread-11:买好了
Thread-11:又准备好了
Thread-11:又买好了
*/
公平锁与非公平锁
ReentrantLock reentrantLock = new ReentrantLock(true/false);//true:公平;false不公平
package com.example.myapplication.demo.lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockDemo2 {
static class ReentrantLockTask{
ReentrantLock lock = new ReentrantLock(false);
void print(){
String name = Thread.currentThread().getName();
try {
lock.lock();
System.out.println(name+"第一次打印");
Thread.sleep(1000);
lock.unlock();
lock.lock();
System.out.println(name+"第二次打印");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
final ReentrantLockTask task = new ReentrantLockTask();
Runnable runnable = new Runnable() {
@Override
public void run() {
task.print();
}
};
for (int i = 0; i < 10; i++) {
new Thread(runnable).start();
}
}
}
/*
Thread-2第一次打印
Thread-2第二次打印
Thread-3第一次打印
Thread-3第二次打印
Thread-4第一次打印
Thread-4第二次打印
Thread-5第一次打印
Thread-5第二次打印
Thread-6第一次打印
Thread-6第二次打印
Thread-7第一次打印
Thread-7第二次打印
Thread-8第一次打印
Thread-8第二次打印
Thread-9第一次打印
Thread-9第二次打印
Thread-10第一次打印
Thread-10第二次打印
Thread-11第一次打印
Thread-11第二次打印
*/
package com.example.myapplication.demo.lock;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockDemo2 {
static class ReentrantLockTask{
ReentrantLock lock = new ReentrantLock(true);
void print(){
String name = Thread.currentThread().getName();
try {
lock.lock();
System.out.println(name+"第一次打印");
Thread.sleep(1000);
lock.unlock();
lock.lock();
System.out.println(name+"第二次打印");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
final ReentrantLockTask task = new ReentrantLockTask();
Runnable runnable = new Runnable() {
@Override
public void run() {
task.print();
}
};
for (int i = 0; i < 10; i++) {
new Thread(runnable).start();
}
}
}
/*
Thread-2第一次打印
Thread-3第一次打印
Thread-4第一次打印
Thread-5第一次打印
Thread-6第一次打印
Thread-7第一次打印
Thread-8第一次打印
Thread-9第一次打印
Thread-10第一次打印
Thread-11第一次打印
Thread-2第二次打印
Thread-3第二次打印
Thread-4第二次打印
Thread-5第二次打印
Thread-6第二次打印
Thread-7第二次打印
Thread-8第二次打印
Thread-9第二次打印
Thread-10第二次打印
Thread-11第二次打印
*/
package com.example.myapplication.demo.lock;
import java.util.Random;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ReentrantLockDemo3 {
static class ReentrantLockTask {
private Condition workerCondition, worker2Condition;
ReentrantLock lock = new ReentrantLock(true);
volatile int flag = 0;
public ReentrantLockTask() {
workerCondition = lock.newCondition();
worker2Condition = lock.newCondition();
}
void work1() {
try {
lock.lock();
if (flag == 0 || flag % 2 == 0) {
System.out.println("worker1 休息会");
workerCondition.await();
}
System.out.println("worker1 搬到的砖是:" + flag);
flag = 0;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
void work2() {
try {
lock.lock();
if (flag == 0 || flag % 2 != 0) {
System.out.println("worker2 休息会");
worker2Condition.await();
}
System.out.println("worker2 搬到的砖是:" + flag);
flag = 0;
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
void boss() {
try {
lock.lock();
flag = new Random().nextInt(100);
if (flag % 2 == 0) {
worker2Condition.signal();
System.out.println("唤醒工人2:"+flag);
}else {
workerCondition.signal();
System.out.println("唤醒工人1:"+flag);
}
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
final ReentrantLockTask lockTask = new ReentrantLockTask();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
lockTask.work1();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true){
lockTask.work2();
}
}
}).start();
for (int i = 0; i < 10; i++) {
lockTask.boss();
}
}
}
/*
worker1 休息会
唤醒工人2:82
worker2 搬到的砖是:82
唤醒工人1:17
worker2 休息会
worker1 搬到的砖是:17
唤醒工人1:71
worker1 搬到的砖是:71
唤醒工人1:59
worker1 搬到的砖是:59
唤醒工人1:77
worker1 搬到的砖是:77
唤醒工人1:87
worker1 搬到的砖是:87
唤醒工人2:54
worker1 休息会
worker2 搬到的砖是:54
唤醒工人2:80
worker2 搬到的砖是:80
唤醒工人2:42
worker2 搬到的砖是:42
唤醒工人2:56
worker2 搬到的砖是:56
worker2 休息会
*/
ReentrantReadWriteLock 共享锁、排他锁
package com.example.myapplication.demo.lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ReentrantLockDemo4 {
static class ReentrantReadWriteLockTask{
private final ReentrantReadWriteLock.ReadLock readLock;
private final ReentrantReadWriteLock.WriteLock writeLock;
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
ReentrantReadWriteLockTask(){
readLock = lock.readLock();
writeLock = lock.writeLock();
}
void read(){
String name = Thread.currentThread().getName();
try {
readLock.lock();
System.out.println("线程"+name+" 正在获取数据...");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
readLock.unlock();
System.out.println("线程"+name+" 释放了读锁...");
}
}
void write(){
String name = Thread.currentThread().getName();
try {
writeLock.lock();
System.out.println("线程"+name+" 正在写入数据...");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
writeLock.unlock();
System.out.println("线程"+name+" 释放了写锁...");
}
}
}
public static void main(String[] args) {
final ReentrantReadWriteLockTask task = new ReentrantReadWriteLockTask();
for (int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
task.read();//因为是读写锁,所以3个线程的日志会一起打印出来
}
}).start();
}
for (int i = 0; i < 3; i++) {
new Thread(new Runnable() {
@Override
public void run() {
task.write();
}
}).start();
}
}
}
/*
线程Thread-2 正在获取数据...
线程Thread-3 正在获取数据...
线程Thread-4 正在获取数据...
线程Thread-3 释放了读锁...
线程Thread-6 正在写入数据...
线程Thread-2 释放了读锁...
线程Thread-4 释放了读锁...
线程Thread-6 释放了写锁...
线程Thread-5 正在写入数据...
线程Thread-5 释放了写锁...
线程Thread-7 正在写入数据...
线程Thread-7 释放了写锁...
*/