diff --git a/hw-04/.gitignore b/hw-04/.gitignore new file mode 100644 index 0000000..6a6df0f --- /dev/null +++ b/hw-04/.gitignore @@ -0,0 +1,61 @@ +# Emacs/vim + +*~ +*.swp + +# VSCode + +.vscode +.classpath +.project +.settings/ + + +# MacOS + +.DS_Store + +# CS56 specific: Secrets files + +localhost.json +heroku.json + +# Maven (from: https://github.com/github/gitignore/blob/master/Maven.gitignore) + +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +# https://github.com/takari/maven-wrapper#usage-without-binary-jar +.mvn/wrapper/maven-wrapper.jar + +# Java (from: https://github.com/github/gitignore/blob/master/Java.gitignore) + +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* diff --git a/hw-04/artifacts/result.pdf b/hw-04/artifacts/result.pdf new file mode 100644 index 0000000..bdb9ea7 Binary files /dev/null and b/hw-04/artifacts/result.pdf differ diff --git a/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-42-20.png b/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-42-20.png new file mode 100644 index 0000000..f0ed90c Binary files /dev/null and b/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-42-20.png differ diff --git a/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-43-01.png b/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-43-01.png new file mode 100644 index 0000000..75d361b Binary files /dev/null and b/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-43-01.png differ diff --git a/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-51-48.png b/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-51-48.png new file mode 100644 index 0000000..97109ce Binary files /dev/null and b/hw-04/artifacts/screenshots/Screenshot from 2026-08-20 17-51-48.png differ diff --git a/hw-04/concurrency/pom.xml b/hw-04/concurrency/pom.xml new file mode 100644 index 0000000..e28074e --- /dev/null +++ b/hw-04/concurrency/pom.xml @@ -0,0 +1,18 @@ + + 4.0.0 + com.example + concurrency + jar + 1.0-SNAPSHOT + concurrency + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + diff --git a/hw-04/concurrency/src/main/java/com/example/DeadLock.java b/hw-04/concurrency/src/main/java/com/example/DeadLock.java new file mode 100644 index 0000000..3382c06 --- /dev/null +++ b/hw-04/concurrency/src/main/java/com/example/DeadLock.java @@ -0,0 +1,47 @@ +package com.example; + +import com.example.util.ThreadUtils; + +public class DeadLock { + + private static final Object LOCK_A = new Object(); + private static final Object LOCK_B = new Object(); + + public static void main(String[] args) { + + Thread thread1 = + new Thread( + () -> { + synchronized (LOCK_A) { + System.out.println("Thread 1: получил LOCK_A"); + + ThreadUtils.sleep(100); + + System.out.println("Thread 1: пытается получить LOCK_B"); + + synchronized (LOCK_B) { + System.out.println("Thread 1: получил LOCK_B"); + } + } + }); + + Thread thread2 = + new Thread( + () -> { + synchronized (LOCK_B) { + System.out.println("Thread 2: получил LOCK_B"); + + ThreadUtils.sleep(100); + + System.out.println("Thread 2: пытается получить LOCK_A"); + + synchronized (LOCK_A) { + System.out.println("Thread 2: получил LOCK_A"); + } + } + }); + + thread1.start(); + thread2.start(); + } +} diff --git a/hw-04/concurrency/src/main/java/com/example/LiveLock.java b/hw-04/concurrency/src/main/java/com/example/LiveLock.java new file mode 100644 index 0000000..678748d --- /dev/null +++ b/hw-04/concurrency/src/main/java/com/example/LiveLock.java @@ -0,0 +1,36 @@ +package com.example; + +import com.example.util.ThreadUtils; + +public class LiveLock { + + private static boolean thread1WantsToPass = true; + private static boolean thread2WantsToPass = true; + + public static void main(String[] args) { + Thread thread1 = + new Thread( + () -> { + while (thread1WantsToPass) { + System.out.println("Поток 1: Я уступаю дорогу Потоку 2..."); + ThreadUtils.sleep(100); + } + + System.out.println("Поток 1 прошел!"); + }); + + Thread thread2 = + new Thread( + () -> { + while (thread2WantsToPass) { + System.out.println("Поток 2: Я уступаю дорогу Потоку 1..."); + ThreadUtils.sleep(100); + } + + System.out.println("Поток 2 прошел!"); + }); + + thread1.start(); + thread2.start(); + } +} diff --git a/hw-04/concurrency/src/main/java/com/example/ThreadUtils.java b/hw-04/concurrency/src/main/java/com/example/ThreadUtils.java new file mode 100644 index 0000000..018fa9f --- /dev/null +++ b/hw-04/concurrency/src/main/java/com/example/ThreadUtils.java @@ -0,0 +1,12 @@ +package com.example.util; + +public class ThreadUtils { + + public static void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} diff --git a/hw-04/concurrency/src/main/java/com/example/TwoThreads.java b/hw-04/concurrency/src/main/java/com/example/TwoThreads.java new file mode 100644 index 0000000..fbaf02b --- /dev/null +++ b/hw-04/concurrency/src/main/java/com/example/TwoThreads.java @@ -0,0 +1,55 @@ +package com.example; + +public class TwoThreads { + + private static final Object lock = new Object(); + private static boolean turnOne = true; + + public static void main(String[] args) { + + Thread thread1 = + new Thread( + () -> { + while (true) { + synchronized (lock) { + while (!turnOne) { + try { + lock.wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + } + + System.out.print("1"); + turnOne = false; + lock.notifyAll(); + } + } + }); + + Thread thread2 = + new Thread( + () -> { + while (true) { + synchronized (lock) { + while (turnOne) { + try { + lock.wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + } + + System.out.print("2"); + turnOne = true; + lock.notifyAll(); + } + } + }); + + thread1.start(); + thread2.start(); + } +}