5 Ağustos 2019 Pazartesi

ExecutorCompletionService Sınıfı

Giriş
Bu sınıfın ExecutorService'ten farkı basitçe şöyle düşünülebilir.
  • ExecutorService = incoming queue + worker threads
  • CompletionService = incoming queue + worker threads + output queue
Açıklaması şöyle. İşler submit() metodu ile verildikten sonra bittikçe poll() veya take() metodu ile alınır.
you submit tasks to this, and it gives you them back in order of completion.
constructor - ExecutorService
ExecutorCompletionService dışarıdan verilen bir ExecutorService kullanır.
Örnek
Şöyle yaparız
ThreadPoolExecutor executor = new ThreadPoolExecutor = ...;
ExecutorCompletionService service = new ExecutorCompletionService(executor);
Örnek
Şöyle yaparız.
ExecutorService pool = Executors.newFixedThreadPool(5);
ExecutorCompletionService<String> service = new ExecutorCompletionService<>
(pool);
poll metodu
take() metodu gibi bitmiş işi döndürür. Eğe bitmiş iş yoksa null döndürür.
Şöyle yaparız.
completionService.poll(100,TimeUnit.MILLISECONDS);
submit metodu
Şöyle yaparız.
Runnable r = ...;
completionService.submit(r);
take metodu
ExecutorCompletionService'e bir dizi iş verilirse, tüm işlerin bitmesini beklemek gerekmez. Biten işler take() metodu ile alınabilir. Eğer bitmiş iş yoksa bloke olur.
Örnek
Şöyle yaparız
ExecutorService service = Executors.newFixedThreadPool(1);
ExecutorCompletionService<Integer> comp = new ExecutorCompletionService<>(service);

comp.submit(task);
// ... I assume you want to submit N tasks.

for (int i = 0; i < N; ++i) {
  Future<Integer> future = comp.take();
  Integer result = future.get();

  // ... do something with the result ...
}
Örnek
Şöyle yaparız.
ExecutorService pool = Executors.newFixedThreadPool(2);
CompletionService<String> service = new ExecutorCompletionService<String>(pool);
final List<? extends Callable<String>> callables = Arrays.asList(
    new SleepingCallable("slow", 5000),
    new SleepingCallable("quick", 500));
for (final Callable<String> callable : callables) {
  service.submit(callable);
}
pool.shutdown();
try {
  while (!pool.isTerminated()) {
    final Future<String> future = service.take();
    System.out.println(future.get());
  }
} catch (ExecutionException | InterruptedException ex) { }



SecureRandom Sınıfı

Giriş
Şu satırı dahil ederiz. Random sınıfından kalıtır.
import java.security.SecureRandom;
import java.util.Random;
Bu sınıf Linux'ta /dev/random dosyasını okuduğu için sistemin entropi havuzu boş ise havuzun dolmasını bekleyebiliyor. Bloke olmayan /dev/urandom dosyasını kullanması için şöyle yaparız.
-Djava.security.egd=file:/dev/./urandom
Kötü Kullanım
Şu kullanımda seed verilmiyor.
SecureRandom random = new SecureRandom();
byte[] key = new byte[16];
random.nextBytes(key);
Şöyle yaparız
SecureRandom random = new SecureRandom();

byte[] seed = new byte[32];
random.nextBytes(seed);
random.setSeed(seed);

byte[] key = new byte[16];
random.nextBytes(key);
Açıklaması şöyle
In this example, the application is using a SecureRandom object to generate a 16-byte key, but the SecureRandom object is not properly seeded with random data. This could potentially make the key predictable and vulnerable to attack.

One way to fix this vulnerability is to properly seed the SecureRandom object with random data, such as by using a hardware random number generator or by collecting entropy from user input or system events. 

in this example, we’re first generating a random seed for the SecureRandom object, and then using that seed to properly seed the
constructor 
Şöyle yaparız.
Random random = new SecureRandom();
constructor - string
Şöyle yaparız.
SecureRandom secureRandomGenerator = SecureRandom.getInstance("SHA1PRNG");
getInstanceStrong metodu
Şöyle yaparız.
// Java 7
SecureRandom random = new SecureRandom();
// Java 8
SecureRandom random = SecureRandom.getInstanceStrong();
ints metodu
Şöyle yaparız
SecureRandom random = new SecureRandom();
IntStream stream = random.ints();
longs metodu
Şöyle yaparız
Iterator<Long> iterator = new SecureRandom().longs.distinct().iterator();
nextBytes metodu
Belirtilen diziyi rastgele byte ile doldurur.
Örnek
Şöyle yaparız.
SecureRandom random = new SecureRandom();
byte bytes[] = new byte[20];
random.nextBytes(bytes);
Örnek
Şöyle yaparız.
Random RANDOM = new SecureRandom();

/**
 * Returns a random salt to be used to hash a password.
 *
 * @return a 16 bytes random salt
 */
public static byte[] getNextSalt() {
  byte[] salt = new byte[16];
  RANDOM.nextBytes(salt);
  return salt;
}