public class PC {
/**
* 题目:生产者-消费者。
* 同步访问一个数组Integer[10],生产者不断地往数组放入整数1000,数组满时等待;消费者不断地将数组里面的数置零,数组空时等待。
*/
private static final Integer[] val=new Integer[10];
private static int size=0;
private static final int MAX_SIZE=10;
public static void main(String[] args) {
PC pc=new PC();
new Thread(pc.new P()).start();
new Thread(pc.new C()).start();
}
public synchronized void consume(){
while(size<=0){
try {
System.out.println(size+"- consume wait");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
val[--size]=0;
notifyAll();
System.out.println("successful consume and now size="+size);
}
public synchronized void produce(int value){
while(size==MAX_SIZE){
try {
System.out.println(size+"- produce wait");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
val[size++]=value;
notifyAll();
System.out.println("successful produce and now size="+size);
}
class P implements Runnable{
public void run(){
while(true)produce(1000);
}
}
class C implements Runnable{
public void run(){
while(true)consume();
}
}
}
分享到:
相关推荐
这种类型的对象可以存储10个20~80之间的整数,即他的内部有一个整型数组存储数据。编程: (1) 判断两个inergerSet类对象S1和S2是否相等。提示:集合相等的前提是所有元素相等。 (2) 输出两个集合对象的交集。 ...
该资源主要介绍了android数组资源string-array integer-array的用法 包括int类型、string类型、文件类型的使用
):设有一个数组A:array [0..N-1] of integer; 存放的元素为0~N-1(1<N<=10)之间的整数,且A[i]≠A[j](i≠j)。例如当N=6时,有:A=(4,3,0,5,1,2)。此时,数组A的编码定义如下: A[0]编码为0; A[i]编码为...
巨大整数创建一个服务HugeInteger,该服务使用40个元素的数字数组存储每个最大40位数字的整数。 提供解析,toString,加法和减法的方法。 方法解析应该接收一个字符串,使用charAt方法提取每个数字,并将每个数字的...
String[] arr = new String[]{"a","b",...Integer[] arr1 = new Integer[]{22,33,44,55,6,7,22,88,999}; searchOne(arr, "a"); searchOne(arr1, 33); 输出结果: a在被查询数组的下标为:0,8 33在被查询数组的下标为:1
正整数数组 验证值是否为正整数数组。安装$ npm install validate.io-positive-integer-array 要在浏览器中使用,请使用 。用法 var isValid = require ( 'validate.io-positive-integer-array' ) ;isValid(值) 验证...
介绍php字符串与byte字节数组转化类,转换一个String字符串为byte数组,将字节数组转化为String类型的数据,转换一个int为byte数组,从字节数组中指定的位置读取一个Integer类型的数据,转换一个shor字符串为byte...
JAVA-int和Integer的区别1.zip
这个类库可以实现 1.转换一个String字符串为byte数组 ...4.从字节数组中指定的位置读取一个Integer类型的数据 5.转换一个shor字符串为byte数组 6.从字节数组中指定的位置读取一个Short类型的数据
从字节数组中指定的位置读取一个Integer类型的数据 5.转换一个shor字符串为byte数组 6.从字节数组中指定的位置读取一个Short类型的数据 需要的朋友可以参考下,方便大家学习php
producer_consumer.java 演示生产者-消费者线程 consumer.java 消费者线程 producer.java 生产者线程 common.java 公有类 第9章 示例描述:本章学习运行时类型识别。 Candy.java 一个用来测试的简单类 ...
数组某一维的元素个数。 3、调试数组方法:本地窗口 4、静态数组、动态数组和多维数组 静态数组:在定义数组时就确定维数和上下界的数组。如,Dim arrTemp(5) As Integer 动态数组:在定义数组时未确定维数和上下界...
VB 6.0 数组参数调用实例: Dim A1(), B1(), C1() '声明数组 Private Sub Form_Load() Dim i As Integer '给数组赋初值 Show A1 = Array(2, 4, 6, 8, 10) '用Array函数赋值 B1 = Array(1, 3, 5, 7, 9, ...
b = Array(10, 20) '如果数组个数有变,可直接在此修改 '以下代码为通用格式,无论a、b数组如何变化,均会得出数组c的值 ReDim c((UBound(a) + 1) * (UBound(b) + 1) - 1) i = -1 For j = 0 To UBound(b) For k = 0 ...
You successfully commissioned a NodeMCU custom build from the release branch.... We'll keep you posted by email about the progress of your build. In the meantime I suggest you take a look at the build ...
[989]数组形式的整数加法|add-to-array-form-of-integer将K转换成为vector 然后逐位相加。
java数组遍历 删除remove。需要的朋友可以过来参考下,希望对大家有所帮助
前端项目-big-integer,An arbitrary length integer library for Javascript
每个生产者可不断地每次往缓冲池中送一个生产产品,而每个消费者则可不断地每次从缓冲池中取出一个产品。指针i和指针j分别指出当前的第一个空缓冲块和第一个满缓冲块。 2.分析和理解 (1)既存在合作同步问题,也...
负整数数组 验证值是否为负整数数组。 安装 $ npm install validate.io-negative-integer-array 要在浏览器中使用,请使用 。 用法 var isValid = require ( 'validate.io-negative-integer-array' ) ; isValid...