0

I have a setup where I have 2 different groups of threads performing 2 separate tasks (for those wondering, group 1 calculates SHA-256 Hashes of strings, and the second group compares those hashes to ones in a dictionary). Logically what I want to happen is have all the group 2 threads created, have them wait, then as each group 1 thread computes a hash have all the group 2 threads "wake up" and check the computed hash to check for a match. This is my code for the group 2 thread using a shared dictionary of computed hashes called "shared"

private static class Group2Th implements Runnable {
    private String dbUser;
    private String dbHashedPass;
    private SharedDict shared;
    public Group2Th(String dbUser, String dbHashedPass, SharedDict shared) {
        this.dbUser = dbUser;
        this.dbHashedPass = dbHashedPass;
        this.shared = shared;
    }

    public void run() {
        System.out.println("Hello from G2 Thread: " + this.dbUser + " ==> " + this.dbHashedPass);
    }
}

How would I go about making this thread wait until something in the "shared" dictionary of hashes gets added?

Clay Benson
  • 161
  • 1
  • 9
  • 3
    see related on the right? –  Feb 20 '14 at 20:38
  • possible duplicate of [How to wait for a set of threads to complete?](http://stackoverflow.com/questions/1252190/how-to-wait-for-a-set-of-threads-to-complete) – jeremyjjbrown Feb 20 '14 at 20:39
  • 1
    The OP doesn't want to wait for threads to complete. Read the question. – JB Nizet Feb 20 '14 at 20:55
  • I did see the questions asked in the related section, but I don't think join() is quite what I was looking for. I apologize if I was not clear in asking my question – Clay Benson Feb 20 '14 at 21:22

2 Answers2

1

This looks like a typical producer/consumer program. It's typically solved by sharing a BlockingQueue among the producer threads and the consumer threads.

Producer threads put values in the queue, and consumer threads get them from the queue. The consumer thread is blocked by the queue until an element is available. The producer threads can also be blocked by the queue if the queue contains too many elements.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Interesting! Didn't know this existed. Appreciate the answer! It looks like if I wanted to make the Queue have a (relatively) indefinite size I could just use a LinkedBlockingQueue, as I don't know how many elements could be in this Queue. – Clay Benson Feb 20 '14 at 21:08
  • Yes. But limiting the size of the queue can be useful to avoid consuming too much memory, and to make sure producers don't add values to the queue at a pace that producers can not sustain. Once the limit is reached, producers will start getting blocked, limiting their pace and making sure memory usage doesn't climb infinitely. – JB Nizet Feb 20 '14 at 21:10
  • There's one logical issue if I were to limit the size though. Lets say there are 200 group 1 threads (I am making a thread per hash computed from a dictionary, so 200 lines == 200 threads). If I limit the size of the queue to 200 and none of those 200 hashes are in the dictionary that group 1 references, then I would theoretically reach a deadlock where all threads would block. – Clay Benson Feb 20 '14 at 21:23
  • I don't really understand what you're trying to achieve, but 200 threads that aren't doing IO are too many. Threads shouldn't be created to perform a single short task. Instead, they should be created onces and be reused to perform many short tasks. Use a thread pool. See `java.util.concurrent.Executors`. – JB Nizet Feb 20 '14 at 21:27
0

Using BlockingQueue we can achieve this task. As per Java Doc "A Queue that additionally supports operations that wait for the queue to become non-empty when retrieving an element, and wait for space to become available in the queue when storing an element. "

More Info enter link description here

Siva Kumar
  • 1,983
  • 3
  • 14
  • 26