Java 线程:如何将检查放在线程使用者内部以进行数据接收检查
我有如下所示的生产者-消费者示例程序 我怎样才能在我的Consumer Thread类中设置一个条件,这样如果我有1分钟没有从producer收到数据,我就需要记录它 这是我的制片人-消费者节目Java 线程:如何将检查放在线程使用者内部以进行数据接收检查,java,multithreading,Java,Multithreading,我有如下所示的生产者-消费者示例程序 我怎样才能在我的Consumer Thread类中设置一个条件,这样如果我有1分钟没有从producer收到数据,我就需要记录它 这是我的制片人-消费者节目 public class ProducerConsumerTest { public static void main(String[] args) { CubbyHole c = new CubbyHole(); Producer p1 = new Produc
public class ProducerConsumerTest {
public static void main(String[] args) {
CubbyHole c = new CubbyHole();
Producer p1 = new Producer(c, 1);
Consumer c1 = new Consumer(c, 1);
p1.start();
c1.start();
}
}
class CubbyHole {
private int contents;
private boolean available = false;
public synchronized int get() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
available = false;
notifyAll();
return contents;
}
public synchronized void put(int value) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) {
}
}
contents = value;
available = true;
notifyAll();
}
}
class Producer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Producer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
while(true)
{
for (int i = 0; i < 100000; i++) {
cubbyhole.put(i);
System.out.println("Producer #" + this.number + " put: " + i);
try {
sleep((int) (Math.random() * 2000));
} catch (Exception e) {
}
}
}
}
}
class Consumer extends Thread {
private CubbyHole cubbyhole;
private int number;
public Consumer(CubbyHole c, int number) {
cubbyhole = c;
this.number = number;
}
public void run() {
while(true)
{
int value = 0;
for (int i = 0; i < 100000; i++) {
value = cubbyhole.get();
System.out.println("Consumer #" + this.number + " got: " + value);
}
}
}
}
公共类ProducerConsumerTest{
公共静态void main(字符串[]args){
长方体孔c=新长方体孔();
生产者p1=新生产者(c,1);
消费者c1=新消费者(c,1);
p1.开始();
c1.开始();
}
}
类隔间{
私有int内容;
private boolean available=false;
公共同步int get(){
while(可用==false){
试一试{
等待();
}捕捉(中断异常e){
}
}
可用=错误;
notifyAll();
返回内容;
}
公共同步的void put(int值){
while(可用==true){
试一试{
等待();
}捕捉(中断异常e){
}
}
内容=价值;
可用=真实;
notifyAll();
}
}
类生成器扩展线程{
私人小房间;
私有整数;
公共制作人(小隔间c,整数){
立方孔=c;
这个数字=数字;
}
公开募捐{
while(true)
{
对于(int i=0;i<100000;i++){
小隔间。放置(i);
System.out.println(“Producer#“+this.number+”put:+i);
试一试{
睡眠((int)(Math.random()*2000));
}捕获(例外e){
}
}
}
}
}
类使用者扩展线程{
私人小房间;
私有整数;
公共消费者(储物间c,整数){
立方孔=c;
这个数字=数字;
}
公开募捐{
while(true)
{
int值=0;
对于(int i=0;i<100000;i++){
value=cubbyhole.get();
System.out.println(“Consumer#“+this.number+”got:“+value”);
}
}
}
}
请任何人提供帮助您可以使用get()
方法并从中登录:
try {
wait(60 * 1000);
if (available == false) {
//log
}
} catch (InterruptedException e) {
}
在消费者跑步中使用方法:
long before;
for (int i = 0; i < 100000; i++) {
before = System.currentTimeMilis();
value = cubbyhole.get();
if (System.currentTimeMilis() - before > 1000 * 60) {
System.out.println("Consumer waited for more than one minute");
}
System.out.println("Consumer #" + this.number + " got: " + value);
}
很久以前;
对于(int i=0;i<100000;i++){
before=System.currentTimeMilis();
value=cubbyhole.get();
如果(System.currentTimeMilis()-before>1000*60){
System.out.println(“消费者等待超过一分钟”);
}
System.out.println(“Consumer#“+this.number+”got:“+value”);
}
谢谢Keepil,但这怎么行呢,因为我想每次都要等1分钟,知道吗??我的要求是,如果直到1分钟日志才从生产者接收到数据,那么在等待时,这将每分钟记录一条消息。如果只想在第一分钟后登录,可以添加一个布尔值以确保只发生一次。