Implementing a dynamic file transfer controller using ThreadPoolExecutor in Java

Recently I had to implement a controller that transfers files from A to B. There are about 8000 files with a size of 1-2 mb each.

  • If one file transfer succeeds, create another stream. (currently increasing corePooleSize +1)
  • If one file transfer fails, close one stream transfer attempt (currently increase corePooleSize -1)
  • If one file transfer failed, do not create another stream for a certain time.

The idea is to get the maximum number of connections / best transmission speed without knowing the host restrictions.

Now, to my question, is ThreadPoolExecutor the best way to implement this behavior, or is there a better way?

//Code simplified
//add all files to callables with type Future<Boolean>
while (true) {

   // entry = get the first result that done.

   if (entry.getValue().get() == Boolean.TRUE) {
       results.remove(entry.getKey());
       if (results.size() > threadPool.getCorePoolSize()) {

           if (System.currentTimeMillis() >= nextAttempt)
               resizeThreadPool(+1);
       }
   } else {
       resizeThreadPool(-1);
       nextAttempt = System.currentTimeMillis() + someTimeinMs;
       entry.setValue(threadPool.submit(entry.getKey()));
   }
   if (results.isEmpty())
      return true;
}

Edit: the minimum and maximum number of threads are specified as a parameter.

+4
source share
1 answer

, (/ - , / , ). , / , . , - / , , , , . , , , , .

, , . , :

  • -.
  • -.
  • / .
  • .
  • ( ).

, , /. /, , ( ). , , , N , N . , , , , . , , , RAM .

+1

Source: https://habr.com/ru/post/1650833/


All Articles