Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit 0361f11

Browse files
author
Kevin Kirn
committed
adds exercise week 3
1 parent b52e363 commit 0361f11

16 files changed

+557
-0
lines changed

week3/exercise/.classpath

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="lib" path="lib/jcip-annotations.jar"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

week3/exercise/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

week3/exercise/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>03_LE_Condition_Synchronization</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
1.58 MB
Binary file not shown.

week3/exercise/WS_Semaphore.pdf

55.1 KB
Binary file not shown.

week3/exercise/src/carpark/Car.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package carpark;
2+
3+
public class Car extends Thread {
4+
private final CarPark carpark;
5+
6+
public Car(String name, CarPark carpark) {
7+
super(name);
8+
this.carpark = carpark;
9+
}
10+
11+
public void run() {
12+
while (true) {
13+
spendSomeTime(10);
14+
log("try to enter carpark");
15+
carpark.enter(); // wait until place is available
16+
spendSomeTime(20);
17+
log("try to exit carpark");
18+
carpark.exit();
19+
}
20+
}
21+
22+
private void spendSomeTime(int max) {
23+
try {
24+
sleep((int) (Math.random() * max * 1000));
25+
} catch (InterruptedException e) {
26+
}
27+
}
28+
29+
private void log(String msg) {
30+
System.out.println(Thread.currentThread().getName() + " " + msg);
31+
}
32+
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package carpark;
2+
3+
public interface CarPark {
4+
void enter();
5+
void exit();
6+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package carpark;
2+
3+
public class CarPark1 implements CarPark {
4+
private int places;
5+
6+
public CarPark1(int places) {
7+
this.places = places;
8+
}
9+
10+
@Override
11+
public synchronized void enter() {
12+
while(places == 0) { } // busy waiting
13+
log("enter carpark");
14+
places --;
15+
}
16+
17+
@Override
18+
public synchronized void exit() {
19+
log("exit carpark");
20+
places++;
21+
}
22+
23+
public static void main(String[] args) {
24+
CarPark cp = new CarPark1(2);
25+
Car c1 = new Car("car1", cp); c1.start();
26+
Car c2 = new Car("car2", cp); c2.start();
27+
Car c3 = new Car("car3", cp); c3.start();
28+
Car c4 = new Car("car4", cp); c4.start();
29+
}
30+
31+
private void log(String msg) {
32+
System.out.println(Thread.currentThread().getName() + " " + msg);
33+
}
34+
35+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package carpark;
2+
3+
4+
public class CarPark2 implements CarPark {
5+
private int places;
6+
7+
public CarPark2(int places) {
8+
this.places = places;
9+
}
10+
11+
private synchronized boolean isFull() {
12+
return places == 0;
13+
}
14+
15+
private synchronized void decPlaces() {
16+
places--;
17+
System.out.println("places: " + places);
18+
}
19+
20+
private synchronized void incPlaces() {
21+
places++;
22+
}
23+
24+
@Override
25+
public void enter() {
26+
while(isFull()) { } // busy waiting
27+
log("enter carpark");
28+
decPlaces();
29+
}
30+
31+
@Override
32+
public void exit() {
33+
log("exit carpark");
34+
incPlaces();
35+
}
36+
37+
public static void main(String[] args) {
38+
CarPark cp = new CarPark2(2);
39+
Car c1 = new Car("car1", cp); c1.start();
40+
Car c2 = new Car("car2", cp); c2.start();
41+
Car c3 = new Car("car3", cp); c3.start();
42+
Car c4 = new Car("car4", cp); c4.start();
43+
}
44+
45+
private void log(String msg){
46+
System.out.println(Thread.currentThread().getName() + " " + msg);
47+
}
48+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package carpark;
2+
3+
import net.jcip.annotations.GuardedBy;
4+
5+
public class CarPark3 implements CarPark {
6+
@GuardedBy("this")
7+
private int places;
8+
9+
public CarPark3(int places){
10+
this.places = places;
11+
}
12+
13+
private boolean isFull(){
14+
return places == 0;
15+
}
16+
17+
@Override
18+
public void enter() {
19+
while (true) {
20+
synchronized (this) {
21+
if (!isFull()) {
22+
log("enter carpark");
23+
places--;
24+
return;
25+
}
26+
}
27+
sleep(10);
28+
}
29+
}
30+
31+
@Override
32+
public synchronized void exit() {
33+
log("exit carpark");
34+
places++;
35+
}
36+
37+
private void sleep(int time) {
38+
try {
39+
Thread.sleep(time);
40+
} catch (InterruptedException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
45+
public static void main(String[] args){
46+
CarPark cp = new CarPark3(2);
47+
Car c1 = new Car("car1", cp); c1.start();
48+
Car c2 = new Car("car2", cp); c2.start();
49+
Car c3 = new Car("car3", cp); c3.start();
50+
Car c4 = new Car("car4", cp); c4.start();
51+
}
52+
53+
private void log(String msg){
54+
System.out.println(Thread.currentThread().getName() + " " + msg);
55+
}
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package carpark;
2+
3+
public class CarPark4 implements CarPark {
4+
private int places;
5+
6+
public CarPark4(int places){
7+
this.places = places;
8+
}
9+
10+
@Override
11+
public synchronized void enter() {
12+
while(places == 0) {
13+
try {
14+
wait();
15+
} catch (InterruptedException e) {
16+
}
17+
}
18+
log("enter carpark");
19+
places--;
20+
}
21+
22+
@Override
23+
public synchronized void exit() {
24+
log("exit carpark");
25+
places++;
26+
notify();
27+
}
28+
29+
public static void main(String[] args){
30+
CarPark cp = new CarPark4(2);
31+
Car c1 = new Car("car1", cp); c1.start();
32+
Car c2 = new Car("car2", cp); c2.start();
33+
Car c3 = new Car("car3", cp); c3.start();
34+
Car c4 = new Car("car4", cp); c4.start();
35+
}
36+
37+
private void log(String msg){
38+
System.out.println(Thread.currentThread().getName() + " " + msg);
39+
}
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package carpark;
2+
3+
public class CarPark5 implements CarPark {
4+
private int places;
5+
6+
public CarPark5(int places) {
7+
this.places = places;
8+
}
9+
10+
private Object lock = new Object();
11+
12+
public void enter() {
13+
synchronized (lock) {
14+
while (places == 0) {
15+
try {
16+
lock.wait();
17+
} catch (InterruptedException e) {
18+
}
19+
}
20+
places--;
21+
}
22+
}
23+
24+
public void exit() {
25+
synchronized (lock) {
26+
places++;
27+
lock.notify();
28+
}
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package interrupt;
2+
3+
public class Test1 {
4+
5+
public static void main(String[] args) {
6+
Thread.currentThread().interrupt();
7+
System.out.println(Thread.interrupted());
8+
try {
9+
Thread.sleep(1000);
10+
System.out.println("ok1");
11+
} catch (InterruptedException e) {
12+
System.out.println("IE: " + Thread.currentThread().isInterrupted());
13+
}
14+
15+
Thread.currentThread().interrupt();
16+
System.out.println(Thread.currentThread().isInterrupted());
17+
try {
18+
Thread.sleep(1000);
19+
System.out.println("ok2");
20+
} catch (InterruptedException e) {
21+
System.out.println("IE: " + Thread.currentThread().isInterrupted());
22+
}
23+
}
24+
25+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package interrupt;
2+
3+
import java.util.concurrent.locks.ReentrantLock;
4+
5+
public class UnlockTest {
6+
7+
public static void main(String[] args) throws InterruptedException {
8+
9+
Thread good = new Thread() {
10+
public void run() {
11+
try {
12+
interrupt();
13+
ReentrantLock lock = new ReentrantLock();
14+
15+
lock.lockInterruptibly();
16+
try {
17+
// do locked
18+
} finally {
19+
lock.unlock();
20+
}
21+
} catch (InterruptedException ex) {
22+
System.out.println("InterruptedException");
23+
}
24+
25+
}
26+
};
27+
28+
good.start();
29+
good.join();
30+
31+
System.out.println("Checkpoint!");
32+
33+
Thread bad = new Thread() {
34+
public void run() {
35+
ReentrantLock lock = new ReentrantLock();
36+
interrupt();
37+
try {
38+
lock.lockInterruptibly();
39+
// do locked
40+
} catch (InterruptedException ex) {
41+
System.out.println("InterruptedException");
42+
} finally {
43+
lock.unlock();
44+
}
45+
}
46+
};
47+
bad.start();
48+
}
49+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package worksheet;
2+
3+
public final class Semaphore {
4+
5+
private int value;
6+
private final Object LOCK = new Object();
7+
8+
public Semaphore(int initial) {
9+
if (initial < 0)
10+
throw new IllegalArgumentException();
11+
value = initial;
12+
}
13+
14+
public int available() {
15+
return value;
16+
}
17+
18+
public void acquire() {
19+
synchronized (LOCK) {
20+
while (value == 0) {
21+
try {
22+
LOCK.wait();
23+
} catch (InterruptedException e) {
24+
// NOP
25+
}
26+
}
27+
28+
value--; // decrement value
29+
}
30+
}
31+
32+
public void release() {
33+
synchronized (LOCK) {
34+
value++; // increment value
35+
LOCK.notify(); // notify a waiting thread
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)