Advertisement
Guest User

misch27

a guest
Apr 9th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Random;
  5.  
  6.  
  7. class CustomThread extends Thread {
  8.  
  9.     private static Map<Character, Integer> globalCharDictionary = new HashMap<>();
  10.     private final static Object monitor = new Object();
  11.  
  12.     private static synchronized void incrementGlobalCharDictionary(Map<Character, Integer> charDictionary) {
  13.         if (globalCharDictionary.size() == 0) {
  14.             String asciiLowercase = Main.getASCIILowercaseArray();
  15.  
  16.             for (char ch : asciiLowercase.toCharArray()) {
  17.                 globalCharDictionary.put(ch, 0);
  18.             }
  19.         }
  20.  
  21.         for (Map.Entry<Character, Integer> characterIntegerEntry : charDictionary.entrySet()) {
  22.             Character key = characterIntegerEntry.getKey();
  23.             Integer value = characterIntegerEntry.getValue();
  24.             Integer globalValue = globalCharDictionary.get(key);
  25.             globalCharDictionary.put(key, (globalValue + value));
  26.         }
  27.     }
  28.  
  29.     private int charCount;
  30.     private char[] charArray;
  31.     private Random rand = new Random();
  32.  
  33.     CustomThread(int charCount, String charArray) {
  34.         this.charCount = charCount;
  35.         this.charArray = charArray.toCharArray();
  36.     }
  37.  
  38.     private Character getRandomChar(char[] charArray){
  39.         return charArray[rand.nextInt(charArray.length)];
  40.     }
  41.  
  42.     @Override
  43.     public void run() {
  44.         Map<Character, Integer> charDictionary = new HashMap<>();
  45.  
  46.         for (char ch : charArray) {
  47.             charDictionary.put(ch, 0);
  48.         }
  49.  
  50.         for (int k = 0; k < charCount; k++) {
  51.             Character randCh = getRandomChar(charArray);
  52.             int chNumber = charDictionary.get(randCh);
  53.             charDictionary.put(randCh, ++chNumber);
  54.         }
  55.         incrementGlobalCharDictionary(charDictionary);
  56.     }
  57. }
  58.  
  59.  
  60. public class Main{
  61.  
  62.     final static int [] allCharsCount = {100, 1000, 10000, 100000, 150000, 250000, 500000, 1000000, 5000000, 10000000, 15000000,
  63.             20000000, 25000000, 30000000, 35000000, 40000000, 45000000, 50000000, 60000000,
  64.             100000000, 500000000, 1000000000, 2000000000};
  65.  
  66.     final static int threadCount = 64;
  67.  
  68.     public static String getASCIILowercaseArray()
  69.     {
  70.         String answer = "";
  71.         char c;
  72.         for (c = 'a'; c <= 'z'; c++)
  73.         {
  74.             answer += c;
  75.         }
  76.         return answer;
  77.     }
  78.  
  79.  
  80.     public static void main(String[] args) throws InterruptedException {
  81.         String asciiLowercase = getASCIILowercaseArray();
  82.  
  83.         for (int i : allCharsCount) {
  84.             int threadCapacity = i/threadCount;
  85.             ArrayList<CustomThread> threadArray = new ArrayList<>();
  86.  
  87.             long start = System.currentTimeMillis();
  88.             for (int j = 0; j < threadCount; j++) {
  89.                 threadArray.add(new CustomThread(threadCapacity, asciiLowercase));
  90.             }
  91.  
  92.             for (CustomThread thread : threadArray) {
  93.                 thread.start();
  94.             }
  95.  
  96.             for (CustomThread thread : threadArray) {
  97.                 thread.join();
  98.             }
  99.             long finish = System.currentTimeMillis();
  100.  
  101.             long timeConsumedMillis = finish - start;
  102.  
  103.             System.out.println("For " + i + " chars needs " + timeConsumedMillis + " milliseconds");
  104.  
  105.         }
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement