-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lecture: finalise challenges for Multi-Threading lesson
closes #846
- Loading branch information
Showing
12 changed files
with
117 additions
and
122 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package threads; | ||
|
||
public record BlueHamster(String name, Tunnel tunnel) implements Hamster {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package threads; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
public interface Hamster extends Runnable { | ||
String name(); | ||
|
||
Tunnel tunnel(); | ||
|
||
default void idle() { | ||
IntStream.range(0, 5).forEach(i -> System.out.println(name() + ": wandering idly")); | ||
} | ||
|
||
default void moveThroughTunnel() { | ||
System.out.println(name() + ": => entering tunnel"); | ||
tunnel().enter(this); | ||
System.out.println(name() + ": => entered tunnel"); | ||
|
||
IntStream.range(0, 2).forEach(i -> System.out.println(name() + ": moving through tunnel")); | ||
|
||
System.out.println(name() + ": leaving tunnel =>"); | ||
tunnel().leave(this); | ||
System.out.println(name() + ": left tunnel =>"); | ||
} | ||
|
||
@Override | ||
default void run() { | ||
idle(); | ||
moveThroughTunnel(); | ||
idle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package threads; | ||
|
||
public class Main { | ||
public static void main(String... args) { | ||
Tunnel tunnel = Tunnel.get(); | ||
|
||
Hamster bh1 = new BlueHamster("bh1", tunnel); | ||
Hamster bh2 = new BlueHamster("bh2", tunnel); | ||
Hamster bh3 = new BlueHamster("bh3", tunnel); | ||
|
||
Hamster rh1 = new RedHamster("rh1", tunnel); | ||
Hamster rh2 = new RedHamster("rh2", tunnel); | ||
|
||
new Thread(bh1).start(); | ||
new Thread(bh2).start(); | ||
new Thread(bh3).start(); | ||
new Thread(rh1).start(); | ||
new Thread(rh2).start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package threads; | ||
|
||
public record RedHamster(String name, Tunnel tunnel) implements Hamster {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package threads; | ||
|
||
public class Tunnel { | ||
private static final Tunnel tunnel = new Tunnel(); | ||
|
||
private Tunnel() {} | ||
|
||
public static Tunnel get() { | ||
return tunnel; | ||
} | ||
|
||
public void enter(Hamster hamster) { | ||
System.out.println("\t in tunnel (" + hamster.name() + ")"); | ||
} | ||
|
||
public void leave(Hamster hamster) { | ||
System.out.println("\t cleared tunnel (" + hamster.name() + ")"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters