feat: complete concurrency homework assignment
This commit is contained in:
@@ -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*
|
||||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 117 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 241 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 190 KiB |
@@ -0,0 +1,18 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.example</groupId>
|
||||||
|
<artifactId>concurrency</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>concurrency</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user