Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.12 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.concurrent.*;
  3.  
  4. /**
  5.  * !!! ATTENTION !!!
  6.  * Please run it with VM arguments: -Xms2g -Xmx8g -Xss8m
  7.  * !!! ATTENTION !!!
  8.  */
  9. public class ArmadcoString {
  10.  
  11.     private static ExecutorService executor = Executors.newFixedThreadPool(32);
  12.  
  13.     public static void main(String[] args) {
  14.         final String str = ArmadcoString.createStringDataSize(2147483639); //AbstractStringBuilder.MAX_ARRAY_SIZE
  15.         System.out.println("size of str = " + str.length() / (1024 * 1024) + " MB");
  16.  
  17.         // 1. one thread
  18.         System.out.println("one thread: ");
  19.         ArmadcoString.runWithPerformance(str, () -> {
  20.             Map<Character, Long> charMap = new ArmadcoString().calculate(str);
  21.             charMap.entrySet().stream().forEach(e -> System.out.print(e + " "));
  22.         });
  23.  
  24.         // 2. multi threads
  25.         System.out.println("\n----\nmulti threads: ");
  26.         final long THREADS = 16;
  27.         ArmadcoString.runWithPerformance(str, () -> {
  28.             try {
  29.                 List<Future<CalculateResult>> calculateResults = new ArrayList<>();
  30.                 Map<Character, Long> result = new HashMap<>();
  31.                 for (long i = 1; i <= THREADS; i++) {
  32.                     long size = str.length();
  33.                     int start = Long.valueOf((i - 1) * size / THREADS).intValue();
  34.                     int end = Long.valueOf(i * size / THREADS).intValue();
  35.                     Future<CalculateResult> calculateResult = new ArmadcoString().calculate(str, start, end, executor);
  36.                     calculateResults.add(calculateResult);
  37.                 }
  38.                 executor.shutdown();
  39.                 while (!executor.isTerminated()) {
  40.                 }
  41.                 for (Future<CalculateResult> calculateResult : calculateResults) {
  42.                     Map<Character, Long> charMap = calculateResult.get().getCharMap();
  43.                     for (Map.Entry<Character, Long> charEntry : charMap.entrySet()) {
  44.                         if (result.get(charEntry.getKey()) == null || result.get(charEntry.getKey()) < charEntry.getValue()) {
  45.                             result.put(charEntry.getKey(), charEntry.getValue());
  46.                         }
  47.                     }
  48.                 }
  49.                 result.entrySet().stream().forEach(e -> System.out.print(e + " "));
  50.             } catch (InterruptedException | ExecutionException e) {
  51.                 e.printStackTrace();
  52.             }
  53.         });
  54.     }
  55.  
  56.     public Map<Character, Long> calculate(final String str) {
  57.         Map<Character, Long> charMap = new HashMap<>(26);
  58.         long count = 0;
  59.         char prevChar = '-';
  60.         int size = str.length();
  61.         for (int i = 0; i < size; i++) {
  62.             char currChar = str.charAt(i);
  63.             count++;
  64.             if (prevChar == currChar) {
  65.                 // do nothing
  66.             } else {
  67.                 if (prevChar != '-' && (charMap.get(prevChar) == null || charMap.get(prevChar) < count)) {
  68.                     charMap.put(prevChar, Long.valueOf(count));
  69.                 } else {
  70.                     // do nothing
  71.                 }
  72.                 count = 0;
  73.             }
  74.             if (i == size - 1 && (charMap.get(currChar) == null || charMap.get(currChar) < count + 1)) {
  75.                 charMap.put(currChar, Long.valueOf(count + 1));
  76.             }
  77.             prevChar = str.charAt(i);
  78.         }
  79.         return charMap;
  80.     }
  81.  
  82.     public Future<CalculateResult> calculate(final String str, final int start, final int end, final ExecutorService executor) {
  83.         final Future<CalculateResult> retVal = executor.submit(
  84.                 () -> {
  85.                     CalculateResult calculateResult = new CalculateResult();
  86.                     Map<Character, Long> charMap = calculateResult.getCharMap();
  87.                     long count = 0;
  88.                     char prevChar = ' ';
  89.                     for (int i = start; i < end; i++) {
  90.                         char currChar = str.charAt(i);
  91.                         count++;
  92.                         if (prevChar == currChar) {
  93.                             // do nothing
  94.                         } else {
  95.                             if (prevChar != ' ' && (charMap.get(prevChar) == null || charMap.get(prevChar) < count)) {
  96.                                 charMap.put(prevChar, Long.valueOf(count));
  97.                             } else {
  98.                                 // do nothing
  99.                             }
  100.                             count = 0;
  101.                         }
  102.                         if (i == end - 1 && (charMap.get(currChar) == null || charMap.get(currChar) < count + 1)) {
  103.                             charMap.put(currChar, Long.valueOf(count + 1));
  104.                         }
  105.                         prevChar = str.charAt(i);
  106.                     }
  107.                     return calculateResult;
  108.                 });
  109.         return retVal;
  110.     }
  111.  
  112.     private static void runWithPerformance(final String str, Runnable runnable) {
  113.         long startTime = System.nanoTime(); // performance
  114.  
  115.         runnable.run();
  116.  
  117.         long endTime = System.nanoTime();
  118.         long ms = (endTime - startTime) / 1000000;
  119.         System.out.println("\ntime = " + ms + " ms");
  120.     }
  121.  
  122.     private static String createStringDataSize(int msgSize) {
  123.         Random r = new Random();
  124.         StringBuilder sb = new StringBuilder(msgSize);
  125.         for (int i = 0; i < msgSize; i++) {
  126.             char c = (char) (r.nextInt(26) + 'a');
  127.             sb.append(c);
  128.         }
  129.         return sb.toString();
  130.     }
  131.  
  132.     static public class CalculateResult {
  133.         private Map<Character, Long> charMap = new HashMap<>(26);
  134.         private Character startChar;
  135.         private int startCharCount;
  136.         private Character endChar;
  137.         private int endCharCount;
  138.  
  139.         public CalculateResult() {
  140.         }
  141.  
  142.         public CalculateResult(Character startChar, int startCharCount, Character endChar, int endCharCount) {
  143.             this.startChar = startChar;
  144.             this.startCharCount = startCharCount;
  145.             this.endChar = endChar;
  146.             this.endCharCount = endCharCount;
  147.         }
  148.  
  149.         public Map<Character, Long> getCharMap() {
  150.             return charMap;
  151.         }
  152.  
  153.         public void setCharMap(Map<Character, Long> charMap) {
  154.             this.charMap = charMap;
  155.         }
  156.  
  157.         public Character getStartChar() {
  158.             return startChar;
  159.         }
  160.  
  161.         public void setStartChar(Character startChar) {
  162.             this.startChar = startChar;
  163.         }
  164.  
  165.         public int getStartCharCount() {
  166.             return startCharCount;
  167.         }
  168.  
  169.         public void setStartCharCount(int startCharCount) {
  170.             this.startCharCount = startCharCount;
  171.         }
  172.  
  173.         public Character getEndChar() {
  174.             return endChar;
  175.         }
  176.  
  177.         public void setEndChar(Character endChar) {
  178.             this.endChar = endChar;
  179.         }
  180.  
  181.         public int getEndCharCount() {
  182.             return endCharCount;
  183.         }
  184.  
  185.         public void setEndCharCount(int endCharCount) {
  186.             this.endCharCount = endCharCount;
  187.         }
  188.     }
  189.  
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement