//I have therefore my boasting in Christ Jesus in things pertaining to God. (John 15:17) package com.javarush.task.task17.task1722; /* ÐоÑÑиÑаем */ public class Solution { public static void main(String[] args) throws InterruptedException { Counter counter1 = new Counter(); Counter counter2 = new Counter(); Counter counter3 = new Counter(); Counter counter4 = new Counter(); counter1.start(); counter1.join(); counter2.start(); counter2.join(); counter3.start(); counter3.join(); counter4.start(); counter4.join(); for (int i = 1; i <= 100; i++) { if (values[i] != 1) { System.out.println("ÐаÑÑив values ÑодеÑÐ¶Ð¸Ñ ÑлеменÑÑ Ð½ÐµÑавнÑе 1"); break; } } } public static Integer count = 0; public static int[] values = new int[105]; static { for (int i = 0; i < 105; i++) { values[i] = 0; } } public static void incrementCount() { count++; } public static int getCount() { return count; } public static class Counter extends Thread { @Override public void run() { do { synchronized (Solution.class) { incrementCount(); values[getCount()]++; } try { Thread.sleep(1); } catch (InterruptedException e) { } } while (getCount() < 100); } } } /* ÐоÑÑиÑаем 1. Сделай Ñак, ÑÑÐ¾Ð±Ñ ÑезÑлÑÑÐ°Ñ ÑÑпел поÑÑиÑаÑÑÑÑ Ð´Ð»Ñ Ð²ÑÐµÑ ÑлеменÑов маÑÑива values ÐРиÑполÑзÑÑ Thread.sleep в меÑоде main(String[] args) 2. ÐÑпÑÐ°Ð²Ñ synchronized блок Ñак, ÑÑÐ¾Ð±Ñ Ð¼Ð°ÑÑив values заполнилÑÑ Ð·Ð½Ð°Ñением 1 ТÑебованиÑ: 1.âÐлаÑÑ Solution должен ÑодеÑжаÑÑ ÐºÐ»Ð°ÑÑ Counter. 2.âÐлаÑÑ Counter должен бÑÑÑ Ð½Ð¸ÑÑÑ. 3.âÐеÑод run() клаÑÑа Counter должен ÑодеÑжаÑÑ synchronized блок. 4.âSynchronized блок меÑода run() не должен блокиÑоваÑÑ Ð¼ÑÑÑÐµÐºÑ this. 5.âÐеÑод main(String[] args) клаÑÑа Solution не должен иÑполÑзоваÑÑ Thread.sleep(). 6.âÐÐ»Ñ ÐºÐ°Ð¶Ð´Ð¾Ð¹ ниÑи, в меÑоде main(String[] args) клаÑÑа Solution иÑполÑзÑй меÑод, коÑоÑÑй Ð¾Ð¶Ð¸Ð´Ð°ÐµÑ Ð·Ð°Ð²ÐµÑÑение ниÑи. package com.javarush.task.task17.task1722; * ÐоÑÑиÑаем * public class Solution { public static void main(String[] args) throws InterruptedException { Counter counter1 = new Counter(); Counter counter2 = new Counter(); Counter counter3 = new Counter(); Counter counter4 = new Counter(); counter1.start(); counter2.start(); counter3.start(); counter4.start(); for (int i = 1; i <= 100; i++) { if (values[i] != 1) { System.out.println("ÐаÑÑив values ÑодеÑÐ¶Ð¸Ñ ÑлеменÑÑ Ð½ÐµÑавнÑе 1"); break; } } } public static Integer count = 0; public static int[] values = new int[105]; static { for (int i = 0; i < 105; i++) { values[i] = 0; } } public static void incrementCount() { count++; } public static int getCount() { return count; } public static class Counter extends Thread { @Override public void run() { do { synchronized (this) { incrementCount(); values[getCount()]++; } try { Thread.sleep(1); } catch (InterruptedException e) { } } while (getCount() < 100); } } } */