简体   繁体   English

阻塞队列+线程+线程执行顺序

[英]Blocking queue+Thread+Sequence of execution of threads

I have a blocking queue which consists of my thread objects. 我有一个由我的线程对象组成的阻塞队列。 For me the order in which these threads were formed is important. 对我来说,这些线程的形成顺序很重要。 Also each thread is associated with a key. 每个线程也与一个键相关联。 So wht I wanted to do was, if a thread for a key is running then all other threads on the key should be blocked. 所以我想做的是,如果某个键的线程正在运行,则该键上的所有其他线程都应被阻止。 But when the thread finishes its execution, the thread with same key in the queue but oldest should be notified and executed. 但是,当线程完成执行时,应通知并执行队列中具有相同键但最旧的线程。 So I was planing to make a blocking queue of hashmap where in hashmap, my key is key and value is my thread object. 所以我打算制作一个哈希表阻塞队列,其中哈希表中的键是键,值是线程对象。 My problem is 1. HOw do I search for thread related to a particular key in my queue. 我的问题是1.如何搜索与队列中特定键相关的线程。 2. How do I notify that thread to execute now. 2.如何通知该线程立即执行。

Here is what I was talking about. 这就是我在说的。 This should give you enough clues. 这应该给您足够的线索。 This is not tested and doesn't take care of synchronization that you might need depending on you use-case. 这未经测试,并且不会根据使用情况照顾您可能需要的同步。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

// E - Element of type key.
public class Design1<E> {

  private BlockingQueue<List<MyThread<E>>> myQueue;

  // Provides a quicker lookup for the threads for a certain key instead of checking all elements in Queue.
  private Map<E, List<MyThread<E>>> myKeyMap;

  public Design1() {
    // Initialize both myQueue and myKeyMap here.
    myQueue = new ArrayBlockingQueue<>(100);
    myKeyMap = new HashMap<>();
  }

  class MyThread<K> extends Thread {
    K key;

    public K getKey() {
      return key;
    }
    public void run() {
      // Process the key

    }
  }

  public void addToQueue(E key) {
    List<MyThread<E>> keyThreads = myKeyMap.get(key);
    if (keyThreads == null) {
      keyThreads = new ArrayList<>();
    }

    // Create new thread for this key
    MyThread<E> t = new MyThread<E>();
    keyThreads.add(t);

    myKeyMap.put(key, keyThreads);

    try {
      // Block if full.
      myQueue.put(keyThreads);
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void processQueue(E key) {
    try {
      while (true) {
        // Fetch elems from Queue - block if empty
        List<MyThread<E>> keyThreads =  myQueue.take();
        E myKey = null;
        while (keyThreads.size() > 0) {
          // Process all the threads for the same key
          MyThread<E> thread = keyThreads.remove(0);
          myKey = thread.getKey();
          thread.start();
        }
        if (myKey != null) {
          // Clean up hashmap entry too.
          myKeyMap.remove(myKey);
        }
      }
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM