-1

I have a class that creates objects from another class, reading the necessary data from a file. Basically, it's a thread that loads the data and adds to a queue. The data it loads is a String and an Integer per time. The problem is that the app will have 2 of those classes, so 2 threads reading from one file and addin on a queue. I'm having a lot of problems with it, so how would be the best method I could do in order to make the 2 classes add objects in the queue?

Thanks!

Zeroz
  • 125
  • 1
  • 8

4 Answers4

1

BlockingQueue

ConcurrentLinkedQueue

Umesh K
  • 13,436
  • 25
  • 87
  • 129
1

the best way will be to use semaphores...... consider using semaphores

here is a link of and example on how to use it....

hope it helps....

0

I think what you are referring to is the typical producer-consumer problem.

Producer/Consumer threads using a Queue should help.

Community
  • 1
  • 1
Nasir
  • 2,984
  • 30
  • 34
0

You're having a synchronization issue. Do you truly need two threads? If so, put the code that adds to the queue into a synchronized block.

Rodney Gitzel
  • 2,652
  • 16
  • 23