java - How to make BlockingQueue to accept multiple types? -
i have class x, class y , class z. if x or y perform specific conditions, should put blockingqueue. class z takes them queue.
i know creating this:
blockingqueue<x,y> bqueue=new arrayblockingqueue<x,y>(length); is illegal. how make properly?
the simplest way allow blockingqueue accept object type:
blockingqueue<object> q = new arrayblockingqueue<>(length); then, on take() operation, of particular class object is:
object o = q.take(); if (o instanceof x) { x x = (x) o; // work x } else if (o instanceof y) { y y = (y) o; // work y } else { // o neither x nor y } if x , y inherited common class or implement common interface, make queue more specific:
blockingqueue<xyinterface> q = new arrayblockingqueue<>(length);
Comments
Post a Comment