prgst
prgst

Reputation: 121

How to take data from object from BlockingQueue?

I have class for an object with all the fields and getters. Now, one thread is putting some data into it, in my case

object = new MyObject(int, int, char, int) 
queue.put(object);

and then puts it into BlockingQueue, then the second thread is taking this object

MyObject toSolve = queue.take();

My question is how to take the data from object to make operations using its ints.

Upvotes: 0

Views: 134

Answers (1)

Anders R. Bystrup
Anders R. Bystrup

Reputation: 16050

Surely you don't actually mean you're using Object? If yes, then I'm guessing your problem is that you put a YourClass on the queue, but get an java.lang.Object out.

If you look at BlockingQueue you'll see it is genericized, so writing something like (notice the <>'s)

BlockingQueue bq = new BlockingQueue<YourClass>();
bq.put( new YourClass( 1 , 2 , 'a' , 42 ) );

then

YourClass yq = bq.take();

will work like a charm, both in terms of compilation and function, and you can use the getters on yq to obtain your int's and char.

Use generics, that's what they're there for.

Cheers,

Upvotes: 1

Related Questions