java 生产者消费者代码_Java生产者和消费者代码

java 生产者消费者代码

This also helps us to understand the concept of synchronised multi-threading in java, the basic work of our code is that the producer will produce a thread and load it into the memory and after producing the producer thread we would be loading the consumer thread to produce a consumable thread which will consume the value of the produce thread.

这也有助于我们理解Java中同步多线程的概念,我们代码的基本工作是,生产者将产生一个线程并将其加载到内存中,并且在产生了生产者线程之后,我们将消费者线程加载到生产消耗性线程,它将消耗生产线程的值。

We would basically be creating two classes named as producer and consumer class.

我们基本上将创建两个名为生产者和消费者类的类。

The producer class will be running a producer thread which is defined as the put method and after running of producer thread we will use consumer class to run the consumer thread which is basically the get method.

生产者类将运行定义为put方法的生产者线程,并且在运行生产者线程之后,我们将使用消费者类来运行消费者线程,该消费者线程基本上是get方法。

Below is the java code written for the same

下面是针对相同代码编写的Java代码

package cp;
class value
{
int n;
int value=0;
synchronized int get()
{
if(value==0)
try
{
wait(1000);
}
catch(InterruptedException e)
{
System.out.println("Exception caught");
}
System.out.println("consumed the value:"+n);
value=0;
notify();
return n;
}
synchronized void put(int n)
{
if(value==1)
try
{
wait(1000);
}
catch(InterruptedException e)
{
System.out.println("exception");
}
this.n=n;
value=1;
System.out.println("produced:"+n);
notify();
}
}
class Produce implements Runnable
{
value first;
Produce(value first)
{
this.first=first; 
new Thread(this,"Produce").start();
}
public void run()
{
int i=0;
while(i<10)
{
first.put(i++);
}
}
}
class Consume implements Runnable
{
value second;
Consume(value second)
{
this.second=second ;
new Thread(this,"Consum").start();
}
public void run()
{
int i=0;
while(i<10)
{
second.get();
i++;
}
} 
}
public class Produceconsume
{
public static void main(String[] args)
{
value first=new value();
new Produce(first);
new Consume(first);
}
}

Output

输出量

produced:0
consumed the value:0
produced:1
consumed the value:1
produced:2
consumed the value:2
produced:3
consumed the value:3
produced:4
consumed the value:4
produced:5
consumed the value:5
produced:6
consumed the value:6
produced:7
consumed the value:7
produced:8
consumed the value:8
produced:9
consumed the value:9

we have used a while loop here to create 10 producer threads and their respective consumer threads. Hope you have understood the basic concept of producer and consumer threads, there are numerous codes available for producer and consumer threads online, once you develop the understanding you can write your own code too. Have a great day ahead and happy learning.

我们在这里使用了while循环来创建10个生产者线程及其各自的使用者线程。 希望您已经了解了生产者线程和使用者线程基本概念 ,在线上有许多代码可用于生产者线程和使用者线程,一旦您了解了,您也可以编写自己的代码。 祝您有美好的一天,学习愉快。

翻译自: https://www.includehelp.com/java-programs/producer-and-consumer-code-in-java.aspx

java 生产者消费者代码

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544551.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何给SpringBoot配置轻松加密?

在实践中&#xff0c;项目的某些配置信息是需要进行加密处理的&#xff0c;以减少敏感信息泄露的风险。比如&#xff0c;在使用Druid时&#xff0c;就可以基于它提供的公私钥加密方式对数据库的密码进行加密。但更多时候&#xff0c;比如Redis密码、MQ密码等敏感信息&#xff0…

secureFX上传文件的时候报错,secureFX崩溃

SecureFX experienced a fatal error and must close.A crash dump file has been......打开注册表&#xff0c;HKEY_CURRENT_MACHINE->SOFTWARE->Vandyke中的secureCRT和secureFX中的license是secureCRT、secureFX的注册信息&#xff0c;不能删除&#xff0c;删除后需要…

C语言将循环小数/有限小数转换为分数

文章目录数学基础编程思路代码数学基础 早在小学的时候我就对循环小数非常感兴趣&#xff0c;加上初中和高中对循环小数可以说有一定基础研究&#xff0c;因此想到写一个将循环下小数转换为分数的程序&#xff0c;非常有意思&#xff0c;并且对初学者来说&#xff0c;它的输入…

c#中将整数转化为字符串_在C#中将字符串转换为字节数组

c#中将整数转化为字符串Prerequisite: How to declare and use byte[] in C#? 先决条件&#xff1a; 如何在C&#xff03;中声明和使用byte []&#xff1f; C&#xff03;中的字符串到字节数组的转换 (String to Byte Array Conversion in C#) In C#, it is possible that a …

升级了 Windows 11 正式版,有坑吗?

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;今天磊哥去公司上班&#xff0c;惊喜的发现 Windows 提示更新了&#xff0c;并且是 Windows 11 正式版&#xff0c;这太让人…

python序列切片

Python中的序列包括字符串、列表、元组&#xff0c;下面以字符串为例进行演示&#xff0c;列表和元组效果同字符串>>> a0123456789 >>> a[0:2] 01 >>> a[3:5] 34 >>> a[-2] 8 >>> a[0:] 0123456789 >>> a[2:] 23456789 …

C语言结构体的应用——万年历

文章目录万年历简述代码万年历简述 万年历——就是输入一个日期可以查询是星期几&#xff0c;这个功能看起来很普通&#xff0c;但是如果用程序时间的话&#xff0c;还是药费一番周折: 我们需要保存一个固定的日期&#xff0c;存放它是星期几&#xff0c;输入一个自定义的日期…

@Value竟然能玩出这么多花样

前言对于从事java开发工作的小伙伴来说&#xff0c;spring框架肯定再熟悉不过了。spring给开发者提供了非常丰富的api&#xff0c;满足我们日常的工作需求。如果想要创建bean实例&#xff0c;可以使用Controller、Service、Repository、Component等注解。如果想要依赖注入某个对…

ruby 生成哈希值_如何检查Ruby中是否存在哈希键?

ruby 生成哈希值We know that Hashes are the instances which are the combination of keys and their values. A particular key in hash may contain more than one value as well. In this article, we will see how we can check or test the presence of a particular ke…

C语言实现线性动态(单向)链表【详细步骤】

文章目录什么是链表为什么不用结构体数组链表的操作创建表删除元素插入元素代码及运行结果什么是链表 链表是数据结构里面的一种&#xff0c;线性链表是链表的一种&#xff0c;线性链表的延伸有双向链表和环形链表。在编程语言中优化数据结构可以在处理大数据时大大降低程序的…

移动前端经验小结

1. 移动端头部标签 head meta <!DOCTYPE html> <!-- 使用 HTML5 doctype&#xff0c;不区分大小写 --> <html lang"zh-cmn-Hans"> <!-- 更加标准的 lang 属性写法 http://zhi.hu/XyIa --> <head><!-- 声明文档使用的字符编码 -->…

再见收费的Navicat!操作所有数据库靠它就够了!

为了快速管理数据库&#xff0c;我们一般都会选择一款顺手的数据库管理工具。Navicat、DataGrip虽然很好用&#xff0c;但都是收费的。今天给大家推荐一款免费、功能强大的数据库管理工具DBeaver&#xff0c;希望对大家有所帮助&#xff01;DBeaver简介 DBeaver是一款开源的数据…

查找两个字符串中相同字符串_使两个字符串相同的最低成本

查找两个字符串中相同字符串Problem statement: 问题陈述&#xff1a; Given two strings string1 and string2 find the minimum cost required to make the given two strings identical. We can delete characters from both the strings. The cost of deleting a characte…

Matlab对指定参数的曲线进行非线性拟合

Matlab拟合曲线的方式 Matlab拟合曲线的方式有很多种&#xff0c;有三次样条插值、线性插值、多项式拟合等等。多项式拟合由于函数由f(x)anxnan−1xn−1...a1xa0f(x)a_nx^na_{n-1}x^{n-1}...a_1xa_0f(x)an​xnan−1​xn−1...a1​xa0​组成&#xff0c;若采用最小二乘法拟合&a…

MyBatis原生批量插入的坑与解决方案!

作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;前面的文章咱们讲了 MyBatis 批量插入的 3 种方法&#xff1a;循环单次插入、MyBatis Plus 批量插入、MyBatis 原生批量插入…

Spring学习(20)--- Schema-based AOP(基于配置的AOP实现) -- 配置切入点pointcut

pointcut&#xff08;切断点&#xff09;表达式&#xff1a; execution(public * *(..)) execution(* set*(..)) execution(* com.xyz.service.AccountService.*(..)) execution(* com.xyz.service..(..)) execution(* com.xyz.service...(..)) within(com.xyz.service.*)…

系统结构图 数据结构_数据结构图简介

系统结构图 数据结构What you are going to learn? 你要学什么&#xff1f; In this article, we learn about the introduction to Graphs in Data Structure and Algorithm. 在本文中&#xff0c;我们将了解图在数据结构和算法中的介绍 。 What are the components in Gra…

Matlab仿真PID控制(带M文件、simulink截图和参数分析)

文章目录0.符号说明1.如何根据连续系统建立差分方程1.1.获取连续系统的传递函数1.2.获取离散系统的传递函数1.3.转换为差分方程2.基本PID控制原理3.比较PID输出&#xff0c;分析参数产生的影响4.改进PID算法&#xff08;遇限削弱积分法&#xff09;5.simulink仿真0.符号说明 y…

再见 Postman!Apifox 才是 YYDS!

作为开软件开发从业者&#xff0c;API 调试是必不可少的一项技能&#xff0c;在这方面 Postman 做的非常出色。但是在整个软件开发过程中&#xff0c;API 调试只是其中的一部分&#xff0c;还有很多事情 Postman 无法完成&#xff0c;或者无法高效完成&#xff0c;比如&#xf…

Python operator.not_()函数与示例

operator.not_()函数 (operator.not_() Function) operator.not_() function is a library function of operator module, it is used to perform "NOT operation" on a given value and returns True if the given value is zero or any falsy value, False, otherw…