标签:Product Java show void class 线程 container 多线程 public
七.线程通信
在生产者与消费者问题中,生产者和消费者共享同一个资源,且二者之间相互依赖,互为条件。
- 对于生产者,没有生产产品前,要通知消费者等待,生产之后要通知消费者消费。
- 对于消费者,消费之后要通知生产者生产新产品以供消费
- synochronized可阻止并发更新同一个共享资源,实现同步。但不能实现线程之间通信。
解决方法:
1.管程法:生产者将生产的数据放入缓冲区,消费者从缓冲取取出数据
点击查看代码
public class guancheng {
public static void main(String[] args) {
Container container = new Container();
new Productor(container).start();
new Consumer(container).start();
}
}
//产品
class Product{
int id;
public Product(int id) {
this.id = id;
}
}
//消费者
class Consumer extends Thread{
Container container;
public Consumer(Container container){
this.container = container;
}
@Override
public void run() {
// 消费
for (int i = 0; i < 100; i++) {
Product product = container.pop();
System.out.println("消费了第"+product.id+"个产品");
}
}
}
//生产者
class Productor extends Thread{
Container container;
public Productor(Container container){
this.container = container;
}
@Override
public void run() {
// 生产
for (int i = 0; i < 100; i++) {
container.push(new Product(i));
System.out.println("生产了第"+i+"个产品");
}
}
}
//缓冲区
class Container{
Product[] products = new Product[100];
// 定义容器大小
int count = 0;
public synchronized void push(Product product){
// 生产者生产产品
if (count == products.length){
// 容器满了,通知消费者消费,停止生产
}
products[count] = product;
// 没有满,生产者生产产品
count++;
}
public synchronized Product pop(){
// 消费者消费产品
if (count == 0){
// 没有产品,通知生产者生产
}
count--;
// 消费者消费
Product product = products[count];
return product;
}
}
2.信号灯法:设置标志位
点击查看代码
public class xinhaodeng {
public static void main(String[] args) {
Show show = new Show();
new Player(show).start();
new Audience(show).start();
}
}
// 生产者
class Player extends Thread{
Show show;
public Player(Show show){
this.show = show;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
if (i%2==0){
show.act("news->"+i);
}else {
show.act("movie->"+i);
}
}
}
}
// 消费者
class Audience extends Thread{
Show show;
public Audience(Show show){
this.show = show;
}
@Override
public void run() {
for (int i = 0; i < 10; i++) {
show.watch();
}
}
}
// 产品
class Show{
String show;
boolean flag = true;
// 表演者表演,观众等待true
// 观众观看,表演者等待false
public synchronized void act(String show){
if (!flag){
// 观众观看,表演者等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("表演者表演了"+show);
this.notifyAll();
// 通知观众观看
this.show = show;
this.flag = !this.flag;
}
public synchronized void watch(){
if (flag){
// 表演者表演,观众等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("观众观看了"+show);
this.notifyAll();
// 通知表演者表演
this.flag = !this.flag;
}
}
标签:Product,Java,show,void,class,线程,container,多线程,public 来源: https://www.cnblogs.com/tao-gak567/p/15362570.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。