0
点赞
收藏
分享

微信扫一扫

Juc_无juc情况

其生 2022-05-26 阅读 73

Synchronized和Lock的区别

Juc_无juc情况_i++

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket{
private int number = 30;
private Lock lock = new ReentrantLock();

public void sale(){
lock.lock();
try{
if(number > 0){
System.out.println(Thread.currentThread().getName() + "\t卖出了" + (number--) + "还有" + number + "张");
}
}catch (Exception e){
e.printStackTrace();
}finally {
lock.unlock();
}

}
}
public class Demo07 {
public static void main(String[] args) {
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();
// new Thread(new Runnable() {
// @Override
// public void run() {
// for (int i = 0; i < 40; i++) {
// ticket.sale();
// }
// }
// },"A").start();
// new Thread(new Runnable() {
// @Override
// public void run() {
// for (int i = 0; i < 40; i++) {
// ticket.sale();
//
// }
// }
// },"B").start();
// new Thread(new Runnable() {
// @Override
// public void run() {
// for (int i = 0; i < 40; i++) {
// ticket.sale();
//
// }
// }
// },"C").start();
}
}

锁是什么,如何判断锁

最基本的生产者消费者

public class Demo09 {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{for (int i = 0;i < 10;i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{for (int i = 0;i < 10;i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
class Data{
private int number = 0;

public synchronized void increment() throws InterruptedException {
if(number != 0){
this.wait();
}
number ++;
System.out.println(Thread.currentThread().getName() + "=>" + number);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
if(number == 0){
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName() + "=>" + number);
this.notifyAll();
}
}

如果不止两个线程的话
if改为while

public class Demo09 {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{for (int i = 0;i < 10;i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{for (int i = 0;i < 10;i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();

new Thread(()->{for (int i = 0;i < 10;i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();

new Thread(()->{for (int i = 0;i < 10;i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
class Data{
private int number = 0;

public synchronized void increment() throws InterruptedException {
while (number != 0){
this.wait();
}
number ++;
System.out.println(Thread.currentThread().getName() + "=>" + number);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
while (number == 0){
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName() + "=>" + number);
this.notifyAll();
}
}


举报

相关推荐

0 条评论