/
n1tron
/
practice07
Обзор
Документация
Войти
/
n1tron
/
practice07
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/org/example/Main.java
48 строк
1 KB
RodinRoman
init
23 май 2025, 02:17
23 май 2025, 02:17
4f61476
Код
Авторство
О чём код?
package org.example; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) throws InterruptedException { Object lock = new Object(); //Counter counter = new Counter(); Counter2 counter2 = new Counter2(); List<Thread> threads = new ArrayList<>(); for (int i = 0; i < 10; ++i) { Runnable task = () -> { for (int j = 0; j < 100; ++j) { long id = Thread.currentThread().getId(); System.out.println("Main thread class id = " + id + " started"); /* synchronized (lock) { int count = counter.getValue(); ++count; try { Thread.sleep(5); } catch (InterruptedException e) { throw new RuntimeException(e); } counter.setValue(count); }*/ counter2.increment(); System.out.println("Main thread class id = " + id + " finished"); } }; threads.add(new Thread(task)); } threads.forEach(Thread::start); for (Thread t: threads) { t.join(); } System.out.println("Counter = " + counter2.getValue()); } }