Article Directory
Lock
Let's look at a simple example of selling tickets (without lock):
package demo1;
public class SaleTicketTest {
public static void main(String[] args) {
// lambda 表达式 ()表示run() 其中"()"中可以写明参数 ,->{} 表示 run后的代码块{}
// 支持 lambda 表达式,必须是接口上有 @FunctionalInterface 注解
// @FunctionalInterface 注解的接口,只能存在一个 抽象方法!
Ticket ticket = new Ticket();
new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket.sale();
}
},"C").start();
}
}
class Ticket{
private Integer num = 40;
public void sale(){
if(num > 0){
num --;
System.out.println(Thread.currentThread().getName()+"卖票,剩余:"+num+"张");
}
}
}
Run a few more times, and the log information obtained is as follows:
The order is messy!
New feature of JDK8: Instructions for use of functional interface @FunctionalInterface
JDK 1.5之前的做法:
Take
synchronized
for locking!
class Ticket{
private Integer num = 40;
public synchronized void sale(){
if(num > 0){
num --;
System.out.println(Thread.currentThread().getName()+"卖票,剩余:"+num+"张");
}
}
}
JDK 1.5 之后的做法:
You can use java.util.concurrent.locks.Lock
classes to implement 加锁
and 释放锁
operate.
package demo1;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class SaleTicketTest3 {
public static void main(String[] args) {
// lambda 表达式 ()表示run() 其中"()"中可以写明参数 ,->{} 表示 run后的代码块{}
// 支持 lambda 表达式,必须是接口上有 @FunctionalInterface 注解
// @FunctionalInterface 注解的接口,只能存在一个 抽象方法!
Ticket3 ticket3 = new Ticket3();
new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket3.sale();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket3.sale();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 40; i++) {
ticket3.sale();
}
},"C").start();
}
}
class Ticket3{
private Integer num = 40;//目标数
private Integer saleNum = 0; //卖出数
// 可重入锁(最实用)
// 调用无参构造,生成 “不公平锁”
// 可以传递参数 ReentrantLock(boolean fair) {sync = fair ? new FairSync() : new NonfairSync();} true为公平锁
// 不公平锁,性能更好,可以插队!
Lock lock = new ReentrantLock();
public void sale(){
lock.lock(); //加锁
//lock.tryLock();
try {
if(num > 0){
num --;
saleNum ++;
System.out.println(Thread.currentThread().getName()+"卖票,共卖了"+saleNum+"张,剩余:"+num+"张");
}
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();//释放锁
}
}
}
The difference between synchronized and Lock
- Synchronized is a java keyword, and Lock is just a class in java.
- Synchronized cannot perceive the state of the lock, but Lock can determine whether the lock is successfully obtained.
- Synchronized locks and releases automatically, but Lock must be manually.
- If the synchronized thread is blocked, other threads will continue to wait; but Lock may not wait (lock.tryLock()).
- synchronized can be reentrant locks, uninterruptible, unfair;
Lock can be reentrant locks, you can determine whether to interrupt, you can choose fair lock/unfair lock. - synchronized is suitable for a small amount of code synchronization problems; Lock is suitable for a large number of synchronized codes.