|
| 1 | + |
| 2 | +//For I testify unto every man that heareth the words of the prophecy of this book, If any man shall add unto these things, |
| 3 | +//God shall add unto him the plagues that are written in this book: (Revelation 22:18) |
| 4 | + |
| 5 | +package com.javarush.task.task29.task2903; |
| 6 | + |
| 7 | +import java.util.concurrent.ConcurrentHashMap; |
| 8 | +import java.util.concurrent.ConcurrentMap; |
| 9 | +import java.util.concurrent.ThreadLocalRandom; |
| 10 | + |
| 11 | +/* |
| 12 | +И еще раз рефакторинг |
| 13 | +*/ |
| 14 | +public class Solution { |
| 15 | + public static final ThreadLocalRandom RANDOM = ThreadLocalRandom.current(); |
| 16 | + |
| 17 | + public static void main(String[] args) { |
| 18 | + ConcurrentMap<Integer, String> concurrentMap = new ConcurrentHashMap<>(); |
| 19 | + for (int i = 0; i < 100; i++) { |
| 20 | + new Thread(getRunnable(i, concurrentMap)).start(); |
| 21 | + } |
| 22 | + sleepASecond(); |
| 23 | + } |
| 24 | + |
| 25 | + private static void sleepASecond() { |
| 26 | + try { |
| 27 | + Thread.sleep(1000); |
| 28 | + } catch (InterruptedException e) { |
| 29 | + e.printStackTrace(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static Runnable getRunnable(final int i, final ConcurrentMap<Integer, String> concurrentMap) { |
| 34 | + return new Runnable() { |
| 35 | + @Override |
| 36 | + public void run() { |
| 37 | + final String name = "Thread #" + i; |
| 38 | + int randomInt = RANDOM.nextInt(20); |
| 39 | + String text = name + " вставил запись для " + randomInt; |
| 40 | + |
| 41 | + //previousEntry is null if it is new entry |
| 42 | + /* instead of null - call concurrentMap.someMethodName(randomInt, text)*/ |
| 43 | + |
| 44 | + String previousEntry = concurrentMap.putIfAbsent(randomInt, text); |
| 45 | + |
| 46 | + if (previousEntry != null) { |
| 47 | + System.out.println(name + " хочет обновить " + randomInt + ", однако уже " + previousEntry); |
| 48 | + } else { |
| 49 | + System.out.println(text); |
| 50 | + } |
| 51 | + } |
| 52 | + }; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/* |
| 57 | +И еще раз рефакторинг |
| 58 | +
|
| 59 | +1. Исправить код в соответствии с Naming and Code Convention (Shift+F6 для рефакторинга) |
| 60 | +
|
| 61 | +2. Просмотри методы класса ConcurrentMap. |
| 62 | +
|
| 63 | +3. В строке "String previousEntry = null;" у concurrentMap вызови метод, который вставит пару (randomInt, text) только для ключа, которого нет в concurrentMap. |
| 64 | +
|
| 65 | +(Вызванный метод должен возвращать предыдущее значение либо null для новой пары.) |
| 66 | +
|
| 67 | +
|
| 68 | +
|
| 69 | +
|
| 70 | +
|
| 71 | +Требования: |
| 72 | +
|
| 73 | +1. Переименуй константу random в соответствии с Naming and Code Convention. |
| 74 | +
|
| 75 | +2. Объект, возвращаемый методом getRunnable, должен быть экземпляром анонимного класса. |
| 76 | +
|
| 77 | +3. Метод run внутри метода getRunnable должен вызывать у concurrentMap метод, вставляющий пару (randomInt, text), если в concurrentMap еще нет пары со значением ключа randomInt. |
| 78 | +
|
| 79 | +4. Метод run класса, возвращаемого методом getRunnable, должен выводить текст на экран. |
| 80 | +*/ |
0 commit comments