0

I am supposed to create 2 Threads. One reads from data from file and creates objects of class Merchandise. The file itself consists of over 10,000 lines:

IdOfMerchandise  Weight

First thread creates Merchandise objects line and every 200 objects it writes about it. The problem I have is, that I need a second thread, working at the same time as the first one, getting these objects and summing up overall weight, writing report every 100 added.

How can i use the thread to get object data at the same time as they are created in the other thread? Is using HashMap good idea to store newly created class objects with 2 variables?

Mifeet
  • 12,949
  • 5
  • 60
  • 108
Jinnn1
  • 43
  • 5

2 Answers2

1

When you pass data from one thread to another thread, you need a thread-safe data structure. As you correctly pointed out, HashMap is not thread-safe. For thread-safe collections in Java, look at the java.util.concurrent package. One of the simplest ways how to implementing a producer-consumer patterns is with LinkedBlockingQueue.

Here is a complete example with two threads, one producing objects, the other one consuming and printing something every 100 objects:

AtomicBoolean finished = new AtomicBoolean(false);
LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>();

Thread thread1 = new Thread(() -> {
    for (int i = 0; i < 10000; i++) {
        String createdObject = Integer.toString(i);
        queue.offer(createdObject);
    }
    finished.set(true);
});

Thread thread2 = new Thread(() -> {
    int count = 0;
    while (!finished.get() || !queue.isEmpty()) {
        try {
            String object = queue.poll(100, TimeUnit.MILLISECONDS);
            if (count++ % 100 == 0) {
                System.out.println(object);
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
});

thread1.run(); thread2.run();
thread1.join(); thread2.join();

You may notice one thing - apart from the produced items, the threads also need to exchange other information - when the producer is finished. Again, you cannot safely exchange this information without synchronization. You can use AtomicBoolean as in the example, or a volatile field.

Community
  • 1
  • 1
Mifeet
  • 12,949
  • 5
  • 60
  • 108
0

Seems like a producer-consumer problem. This official link will help you understanding and implementing the concept.

Guarded Blocks

The basic idea is, consumer can not consume unless producer has produced something.

java8.being
  • 464
  • 3
  • 11
  • 1
    I would stay away from "Guarded Blocks" for two reasons. (1) The whole wait()/notify() thing is just an endless source of confusion for Java noobs. I know, the whole point of the tutorial is to help them get it right, but... (2) If you're a noob, and you think you need a guarded block, then chances are good that you are re-inventing something the library gives you for free. Like, in this case, a `BlockingQueue`. – Solomon Slow May 03 '16 at 21:03