方法名 | 作用 |
---|---|
wait() | 表示线程一直等待,直到其他线程通知,与sleep不同,会释放锁 |
wait(long timeout) | 指定等待的毫秒数 |
notify() | 唤醒一个处于等待状态的线程 |
notifyAll() | 唤醒同一个对象上所有的调用wait()方法的线程,优先级别高的线程优先调用 |
注意:以上方法均是Object类的方法,都只能在同步方法或者同步代码块中使用,否则会抛出异常IegalMonitorStateException
- 生产者将生产好的数据放入缓冲区,消费者从缓冲区拿出数据
while (index <= 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有满,就放入产品
products[count]=product;
count++;
System.out.println("生产了第"+product.id+"个产品");
//正常消费
count--;
Product product=products[count];
System.out.println("消费了-->第"+product.id+"个产品");
//生产者放入产品
public synchronized void push(Product product) {
//如果容器满了,就需要等待消费
while(count==products.length) {
//通知消费者消费,停止生产
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//如果没有满,就放入产品
products[count]=product;
count++;
System.out.println("生产了第"+product.id+"个产品");
//通知消费者消费
this.notifyAll();
}
//消费者消费产品
public synchronized Product pop() {
//判断是否能够消费
while(count==0) {
//等待生产者生产,消费者等待
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//正常消费
count--;
Product product=products[count];
System.out.println("消费了-->第"+product.id+"个产品");
//消费完了,通知生产者生产
this.notifyAll();
return product;
}
/**
* @author 缘友一世
* date 2022/11/12-13:27
*/
public class TestProducerConsumer {
public static void main(String[] args) {
SynContainer container = new SynContainer();
new Producer(container).start();
new Consumer(container).start();
}
}
class Producer extends Thread {
//Container
/*Container 类表示容器,它是一种特殊的组件,可以用来容纳其他组件,
Container 又分为两种类型,分别是 Window 和 Panel;*/
SynContainer container;
public Producer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for(int i=1;i<=20;i++) {
container.push(new Product(i));
}
}
}
class Consumer extends Thread {
SynContainer container;
public Consumer(SynContainer container) {
this.container = container;
}
@Override
public void run() {
for(int i=1;i<=20;i++) {
container.pop();
}
}
}
class Product {
int id;
public Product(int id) {
this.id = id;
}
}
class SynContainer {
//需要一个容器的大小
Product[] products=new Product[10];
//容器技术器
int count=0;
//生产者放入产品
public synchronized void push(Product product) {
//如果容器满了,就需要等待消费
while(count==products.length) {
//通知消费者消费,停止生产
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//如果没有满,就放入产品
products[count]=product;
count++;
System.out.println("生产了第"+product.id+"个产品");
//通知消费者消费
this.notifyAll();
}
//消费者消费产品
public synchronized Product pop() {
//判断是否能够消费
while(count==0) {
//等待生产者生产,消费者等待
try {
this.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
//正常消费
count--;
Product product=products[count];
System.out.println("消费了-->第"+product.id+"个产品");
//消费完了,通知生产者生产
this.notifyAll();
return product;
}
}
/**
* @author 缘友一世
* date 2022/11/12-18:00
*/
//生产者和消费者模型-->利用缓冲区解决:管城法
public class TestPW {
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<10;i++) {
if(i%2==0) {
this.tv.play("庆余年");
}else {
this.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;//表演的节目
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=!this.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;
}
}
/**
* @author 缘友一世
* date 2022/11/12-18:31
*/
public class TestPool {
public static void main(String[] args) {
//1 创建服务,创建线程池。
ExecutorService service = Executors.newFixedThreadPool(5);
//执行
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
}
}
class MyThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* @author 缘友一世
* date 2022/11/12-18:36
*/
public class TestReview {
public static void main(String[] args) throws ExecutionException, InterruptedException {
new MyThread1().start();
new Thread(new MyThread2()).start();
FutureTask<Integer> futureTask = new FutureTask<>(new MyThread3());
new Thread(futureTask).start();
System.out.println(futureTask.get());
}
}
//1 继承Thread类
class MyThread1 extends Thread {
@Override
public void run() {
System.out.println("myThread1-->继承Thread类");
}
}
//2 实现Runnable接口
class MyThread2 implements Runnable {
@Override
public void run() {
System.out.println("myThread2-->实现Runnable接口");
}
}
//3 实现Callable接口
class MyThread3 implements Callable<Integer> {
@Override
public Integer call() throws Exception {
System.out.println("myThread3-->实现Callable接口");
return 1;
}
}