• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read and write  RSS feed

 
Sameera Abeysinghe
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
First I have to tell you my thread knowledge is not good. This is my question.
Class A implements Runnable
Class A has a static jAbstractQueue
Class A reads objects from the Queue

Class B implants Runnable
Class B writes to the Queue

Now my question is
I know that I have to write before I can read, lets say class B has been writing for some time and now writing the 3 or 4 element. So there are defiantly elements to class A to read
So my question is

I and o can be any integers but cant be same and has to be i >o

while class B is writing to location i can class A read from location o at the same time


Thank You
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The blocking queue (in JDK 5) is exactly what you need. One (or many) threads can put items into the queue and another (or many) can read them from the queue. The "blocking" bit in the name means if the queue is empty a reader will block (wait) until another thread puts something in. You can configure it so a writer will also block if the queue is full. If you don't have JDK 5 look at the Apache Commons Thread Pool - they have a dead simple blocking queue.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!