【问题标题】:Threads: How to put a check inside Thread Consumer for Data reciving check线程:如何在线程使用者中进行检查以进行数据接收检查
【发布时间】:2013-05-23 21:30:03
【问题描述】:

我有这个生产者消费者示例程序如下所示

如何在我的消费者线程类中放置一个条件,这样如果我在 1 分钟内没有从生产者那里收到数据,我需要记录它??

这是我的生产者消费者计划

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);
        }
        }
    }
}

谁能帮忙

【问题讨论】:

    标签: java multithreading


    【解决方案1】:

    您可以使用Object#wait(long timeout) 并从get() 方法内部登录:

    try {
        wait(60 * 1000);
        if (available == false) {
            //log
        }
    } catch (InterruptedException e) {
    }
    

    【讨论】:

    • 感谢 Keepil ,但是这怎么能工作,因为我猜它每次都会等待 1 分钟知道吗?我的要求是,如果直到 1 分钟日志才从生产者那里收到数据
    • 这将在等待时每分钟记录一条消息。如果您只想在第一分钟后记录,您可以添加一个布尔值以确保它只发生一次。
    【解决方案2】:

    在您的Consumer run 方法中使用System.currentTimeMilis():

    很久以前;
    for (int i = 0; i  1000 * 60) {
                System.out.println("消费者等待超过一分钟");
            }
            System.out.println("消费者#" + this.number + "得到:" + value);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      相关资源
      最近更新 更多