java - How to implementing BlockingQueue with Custom Comparator with ThreadExecutor? -


i trying run task based on length of string in ascending order. not working expected. here code have tried till now:

import java.util.comparator; import java.util.concurrent.blockingqueue; import java.util.concurrent.priorityblockingqueue; import java.util.concurrent.threadpoolexecutor; import java.util.concurrent.timeunit;  public class priorityqueuetest {     public static void main(string... args) throws interruptedexception {         blockingqueue<runnable> pq = new priorityblockingqueue<runnable>(5,                 new priorityqueuecomparator());         runner r1 = new runner("abc");         runner r2 = new runner("ab");         runner r3 = new runner("abcd");          runner[] arr = new runner[] { r1, r2, r3 };          threadpoolexecutor pool = new threadpoolexecutor(3, 3, 0,                 timeunit.seconds, pq);          (int = 0; < arr.length; i++) {             pool.execute(arr[i]);         }         pool.shutdown();      } }  class priorityqueuecomparator<t extends runner> implements comparator<t> {      public int compare(runner o1, runner o2) {         if (o1.getname().length() < o2.getname().length()) {             return 1;         }         if (o1.getname().length() > o2.getname().length()) {             return -1;         }         return 0;     }  }  class runner implements runnable {     private string name;      public runner(string sname) {         this.name = sname;     }      public void run() {         system.out.println(name);     }      public string getname() {         return name;     }  } 

i expected ouput

ab abc abcd 

or

abcd abc ab 

based on compareto() method of customer comparator?

i guess custom comparator not getting called.

please help.

a priorityqueue sorts tasks in queue @ moment. doesn't

  • sort tasks have started.
  • sort tasks haven't been added.
  • change order tasks complete if have multiple threads.

if have small number of short lived tasks , multiple threads. shouldn't expect see difference.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -