Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.concurrent.ExecutionException;
  5. import java.util.concurrent.ExecutorService;
  6. import java.util.concurrent.Executors;
  7. import java.util.concurrent.Future;
  8.  
  9. public class Main {
  10. private static final char[] alphabet = "eaistnrulodmpcvqgbfjhzxykw0123456789!@#$%&*".toCharArray();
  11. public static void main(String[] args) throws ExecutionException, InterruptedException {
  12. int cores = Runtime.getRuntime().availableProcessors();
  13. System.out.println("Number of cores: " + cores); //8 cores
  14.  
  15. int partitionSize = alphabet.length / cores;
  16. ExecutorService service = Executors.newFixedThreadPool(cores);
  17. List<Future> futures = new ArrayList<Future>();
  18.  
  19. for (int c = 0; c < cores; c++) {
  20.  
  21. char[] part = Arrays.copyOfRange(alphabet, c * partitionSize, (c + 1) * partitionSize);
  22. futures.add(service.submit(new BruteWorker(part)));
  23.  
  24. }
  25.  
  26. for(Future f : futures)
  27. f.get();
  28.  
  29. service.shutdown();
  30. System.out.println("Completed normally");
  31. }
  32. public static class BruteWorker implements Runnable {
  33. private char[] partition;
  34.  
  35.  
  36. BruteWorker(char[] partition) {
  37. this.partition = partition;
  38. }
  39.  
  40. public void run() {
  41. System.out.println("New thread (id = "+ Thread.currentThread().getId() +")");
  42. for (long nbTries = 0; nbTries < 1_000_000_000L; nbTries ++ ) {
  43. if((nbTries % 10_000_000) == 0){
  44. System.out.println(nbTries + " tries on thread id = "+ Thread.currentThread().getId());
  45. }
  46. }
  47. System.out.println("End of thread (id = "+ Thread.currentThread().getId() +")");
  48. }
  49. }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement