0

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.

specializt
  • 1,913
  • 15
  • 26
prgst
  • 121
  • 1
  • 1
  • 11
  • you said you had getters.... use the getters? – Arnaud Potier May 06 '15 at 12:38
  • the above code does not compile - there is no constructor for `(int, int, char, int)` – specializt May 06 '15 at 12:42
  • the above comment is just single lines of code... I thought it was obvious I had a class Object with a constructor... – prgst May 06 '15 at 13:20
  • that ... doesnt make any sense whatsoever – specializt May 06 '15 at 13:25
  • because your sentence doesnt make sense. `Object` is a JVM/JRE/JDK class, you cant create your own in a global / public scope and SO-comments cant include much more than a few sentences, additionally : it would'nt make sense to write more in this very case. Are you confused, perhaps? – specializt May 06 '15 at 13:28
  • Ahhh, true, my bad... I had in mind something different, I meant Object==MyObject class from post. I should use here non-confusing names. Sorry :) – prgst May 06 '15 at 13:31

1 Answers1

1

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,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55