Lock锁
ReentrantLock lock=new ReentrantLock();
class TestLock2 implements Runnable{
int ticketName=10;
//定义lock锁
private final ReentrantLock lock=new ReentrantLock();
@Override
public void run() {
while (true){
try {
lock.lock();//加锁
if (ticketName>0){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(ticketName--);
}else {
break;
}
}finally {
lock.unlock();//解锁
}
}
}
}
wait(); //表示线程一直被等待直到其他线程通知,与sleep不同,会释放锁
wait(long timeout); 指定一个等待的时间
notify() ; 唤醒一个处于等待状态的锁
notifyAll(); 唤醒同一个对象所有调用wait();方法的线程,优先级别高的线程优先调度
线程通信
管程法
wait(); //表示线程一直被等待直到其他线程通知,与sleep不同,会释放锁
wait(long timeout); 指定一个等待的时间
notify() ; 唤醒一个处于等待状态的锁
notifyAll(); 唤醒同一个对象所有调用wait();方法的线程,优先级别高的线程优先调度
//线程通信
//生产者与消费者
public class TestPC {
public static void main(String[] args) {
SynContainer synContainer=new SynContainer();
new Productor(synContainer).start();
new Consumer(synContainer).start();
}
}
//被生产的食物
class Chicken{
public int id;
Chicken(int id){
this.id=id;
}
}
//生产者
class Productor extends Thread{
SynContainer synContainer;
Productor(SynContainer synContainer){
this.synContainer=synContainer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("生产了"+i+"只鸡");
synContainer.push(new Chicken(i));
}
}
}
//消费者
class Consumer extends Thread{
SynContainer synContainer;
Consumer(SynContainer synContainer){
this.synContainer=synContainer;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("消费了"+synContainer.pop().id+"只鸡");
}
}
}
// 容器
class SynContainer{
//定义容器的大小,(能装几只鸡)
Chicken[] chickens=new Chicken[10];
//容器的计数器
int cont=0;
//消费者放入产品
public synchronized void push(Chicken chicken){
//如果产品满了,等待消费者消费
if (cont==chickens.length){
//产品满了 ,停止放入产品
try {
this.wait();
} catch (InterruptedException e) {
}
}else {
chickens[cont] = chicken;
cont++;
this.notify();
}
}
//消费者产品
public synchronized Chicken pop(){
Chicken chicken=null;
//如果没有产品,就等待生产者放入产品
if (cont==0){
//没有产品,通知生产者
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
cont--;
chicken = chickens[cont];
this.notify();
return chicken;
}
}
信号灯法
//生产者消费者问题2,信号灯法,立标志位解决标签:tv,void,线程,new,多线程,public,synContainer From: https://www.cnblogs.com/wukangxiang/p/17284510.html
public class TestPc2 {
public static void main(String[] args) {
Tv tv=new Tv();
new Player(tv).start();
new Watcher(tv).start();
}
}
//生产者===演员
class Player extends Thread{
Tv tv;
public Player(Tv tv){
this.tv=tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i%2==0){
tv.play("快乐大本营");
}else {
tv.play("嗓子疼吃金嗓子");
}
}
}
}
//消费者===观众
class Watcher extends Thread{
Tv tv;
public Watcher(Tv tv){
this.tv=tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
tv.watch();
}
}
}
//产品==节目
class Tv{
String voice;
//flag标志位 演员表演,观众等待true
//观众观看,演员等待 false
boolean flag=true;
//生产节目
public synchronized void play(String voice){
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("演员表演了"+voice);
//通知观众观看
this.notifyAll();
this.voice=voice;
this.flag=!flag;
}
//消费节目
public synchronized void watch(){
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("观众观看了"+voice);
//通知演员生产
this.notifyAll();
this.flag=!flag;
}
}