等待通知多线程生产者消费者问题

时间:2015-07-05 14:58:48

标签: java multithreading wait producer-consumer notify

我正在研究一个基本的线程生产者消费者问题。

现在在我的代码中我假设的是 1)线程最初将进入等待状态,任何任务到来的那一刻都将被通知,它将处理该任务,然后再次等待,但我的线程将突然进入可运行状态。我的理解是否正确?

public static void main(String[] args) {
    AsyncTaskExecutorImpl executorImpl = new AsyncTaskExecutorImpl(10, 5);

    for (int i = 0; i < 200; i++) {

        Runnable task = new createTask();
        System.out.println("Added task no" + i);
        executorImpl.execute(task, 10);
    }
}



import java.util.concurrent.ArrayBlockingQueue;

public class MyArrayBlockingQueue<T> {
private volatile ArrayBlockingQueue<Runnable> internalTaskQueue = new ArrayBlockingQueue<Runnable>(
        10);


public boolean isEmpty() {
    synchronized (this) {
        return internalTaskQueue.isEmpty();
    }
}

public void add(Runnable paramRunnable) throws InterruptedException {
    synchronized (this.internalTaskQueue) {
        this.internalTaskQueue.put(paramRunnable);
        this.internalTaskQueue.notifyAll();

    }

    for (Thread t : Thread.getAllStackTraces().keySet()) {
        if (t.getName().startsWith("T") || t.getName().startsWith("M")) {
            System.out.println(t.getName() + "----" + t.getState());
        }
    }

}

public Runnable poll() {

    Runnable task = null;
    try {
        synchronized (this.internalTaskQueue) {
            while (this.internalTaskQueue.isEmpty()) {

                this.internalTaskQueue.wait();

            }
            task = this.internalTaskQueue.poll();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return task;
}
}`
 import java.util.concurrent.Callable;
 import java.util.concurrent.Future;

 import org.springframework.core.task.AsyncTaskExecutor;

 public class AsyncTaskExecutorImpl implements AsyncTaskExecutor {

private  MyArrayBlockingQueue<Runnable> taskQueue= new MyArrayBlockingQueue<Runnable>();

// Here we are creating a Thread pool of number of threads required
public AsyncTaskExecutorImpl(int no_of_threads, int taskQueueSize) {

    for (int i = 0; i < no_of_threads; i++) {
        IndividualThread thread = new IndividualThread(this.taskQueue);
        thread.start();
    }

    for (Thread t : Thread.getAllStackTraces().keySet()) {
        if (t.getName().startsWith("T") || t.getName().startsWith("M")) {
            System.out.println(t.getName() + "----" + t.getState());
        }
    }
}

@Override
public void execute(Runnable paramRunnable, long paramLong) {
    if (paramRunnable instanceof Runnable) {

        // pick any thread from the threadpool and then execute that
        try {
            this.taskQueue.add(paramRunnable);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}`
class CreateTask implements Runnable {
@Override
public void run() {

    System.out.println(Thread.currentThread().getName() + "got the task");

}

1 个答案:

答案 0 :(得分:0)

NotifyAll会“唤醒”所有等待的线程。但它应该在逻辑上是正确的,因为它们将竞争进入“同步”块,获得访问的第一个线程将发现'非空',拉动数据,退出'同步'块。 然后一些其他线程将进入同步块,但到那时它将看到'空'&amp;立即回到等待(除非有几个'添加'动作,当然,在这种情况下,几个线程会看到'不为空')。换句话说,如果您的自旋锁设计正确,线程将在短时间内变为Runnable。

还有'Object.notify'只唤醒一个线程,但是AFAIK被认为对你的自旋锁不安全。