task on java.util concurrent. Multithreading problems with polling from BlockingQueue -


i've received task java.util.concurrent package of java. i've made totally, there bug or mistake. when queue empty , operator waits 5 seconds method poll should retrieve null , pass operator , operator goes home. doesn't happen. retrieves null doesn't pass operator. sorry english.)

public class client extends thread {  public countdownlatch latch=new countdownlatch(1);  private boolean waiting;  private final random random=new random();   public boolean iswaiting() {     return waiting; }  public void setwaiting(boolean iswaiting) {     this.waiting = iswaiting; }  private static final logger logger;   static {     logger = logger.getlogger(client.class);     new domconfigurator().doconfigure("log4j.xml",             logmanager.getloggerrepository());     logger.setlevel(level.info); }  private int limittime=new random().nextint(5000);  public void run(){     clientqueue.enqueueclient(this);     while(waiting){         if (random.nextboolean()){             try {                 latch.await(5, timeunit.seconds);                 if (!waiting) return;                 clientqueue.removefromqueue(this);                 reporttiredtowait();                 sleep(random.nextint(1000)+500);                 clientqueue.enqueueclient(this);                 reportdecidedtocallagain();             } catch (interruptedexception e) {                 logger.info("exception");             }         }     } }  public client(string name) {     super(name);     this.waiting=true; }  private void reporttiredtowait(){     logger.info("client "+getname()+" tired wait , decided hang up"); }  private void reportdecidedtocallagain(){     logger.info("client "+getname()+" decided call again"); }  @override public string tostring() {     return "client "+getname(); } 

}

    public class clientqueue {      private static final logger logger;       static {         logger = logger.getlogger(clientqueue.class);         new domconfigurator().doconfigure("log4j.xml",                 logmanager.getloggerrepository());         logger.setlevel(level.info);     }      private static clientqueue instance;      private blockingqueue<client> queue;      public static void printqueue(){         system.out.println("list of clients:");         (client client :clientqueue.getinstance().queue){             system.out.println("client "+client.getname());         }         system.out.println("end of list of clients:");      }      private static clientqueue getinstance()     {         if ( instance == null )         {             instance = new clientqueue();         }         return instance;     }      private clientqueue()     {         this.queue = new linkedblockingqueue<client>();     }      public static void enqueueclient(client client){         getinstance().queue.add(client);         reportclientenqueued(client.getname());     }      public static void removefromqueue(client client){         clientqueue.getinstance().queue.remove(client);         reportclientdeletedfromqueue(client.getname());     }      public static client pollfirst(long time, timeunit timeunit) throws interruptedexception{         client client=null;             client = getinstance().queue.poll(time, timeunit);          if (client!=null){             reportclientretrievedfromqueue(client.getname());         }         return client;     }      private static  void reportclientenqueued(string name){         logger.info("client "+name+" put on waiting list");     }      private static void reportclientdeletedfromqueue(string name){         logger.info("client " +name+" deleted waiting list");     }      private static void reportclientretrievedfromqueue(string name){         logger.info("client " +name+" retrieved waiting list");     }  }      public class operator extends thread{      private static final logger logger;      static {         logger = logger.getlogger(operator.class);         new domconfigurator().doconfigure("log4j.xml",                 logmanager.getloggerrepository());         logger.setlevel(level.info);     }      private boolean running;      public operator(string name){         super(name);         running= true;     }      @override     public void run() {         while (running){                 client client=null;                 try {                     client = clientqueue.pollfirst(5, timeunit.seconds);                 } catch (interruptedexception e1) {                     e1.printstacktrace();                 }                 if (client!=null){                     string clientname=client.getname();                     reportoperatorreceivedcall(clientname);                     try {                         client.setwaiting(false);                         client.latch.countdown();                         sleep(10000);                         reportoperatorfinishedconversation(clientname);                     } catch (interruptedexception e) {                         logger.error(e);                     }                 } else{                     reportoperatorfinishedhiswork();                     running=false;                 }         }     }      private void reportoperatorreceivedcall(string name){         logger.info("operator "+getname()+" received call client "+name);     }      private void reportoperatorfinishedconversation(string name){         logger.info("operator "+getname()+" finished conversation client "+name);     }      private void reportoperatorfinishedhiswork(){         logger.info("operator "+getname()+" finished work today, tired , decided go home.");     }   }      public class main {      public static void main(string[] args) {         scheduledexecutorservice executor = executors.newscheduledthreadpool(10);           linkedlist<client> clientlist = new linkedlist<client>();         clientlist.add(new client("vasya"));         clientlist.add(new client("tanya"));         clientlist.add(new client("petya"));         clientlist.add(new client("kolya"));         clientlist.add(new client("elena"));         clientlist.add(new client("anna"));         for(int = 0; < clientlist.size(); i++) {             executor.schedule(clientlist.get(i),  i, timeunit.seconds);         }          linkedlist<operator> operatorlist = new linkedlist<operator>();         operatorlist.add(new operator("bob"));         operatorlist.add(new operator("sandra"));         operatorlist.add(new operator("john"));         for(int = 0; < operatorlist.size(); i++) {             executor.schedule(operatorlist.get(i), 500, timeunit.milliseconds);         }     } } 

you have semicolon in clientqueue.pollfirst. here corrected:

public static client pollfirst(long time, timeunit timeunit) throws interruptedexception{     client client=null;         client = getinstance().queue.poll(time, timeunit);      if (client!=null) { // removed semicolon line         reportclientretrievedfromqueue(client.getname());     }     return client; } 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -