Compare commits
10 Commits
a94e1adc68
...
e3c1fa0f12
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e3c1fa0f12 | ||
| 717dd7835e | |||
|
|
519152a7b9 | ||
| 8c12a1a904 | |||
| ab6ad3dcc7 | |||
|
|
c62e6a5aa1 | ||
|
|
dad6903407 | ||
| 12706d9ec3 | |||
| 0334a99776 | |||
| 661c10c2b1 |
69
README.md
69
README.md
@@ -2,33 +2,54 @@
|
||||
All things Java<br/>
|
||||
|
||||
<h3>Session 03 - Concurrency - Processes and Threads</h3>
|
||||
<ul>
|
||||
<li>ProcessDemo</li>
|
||||
<li>ThreadsDemo</li>
|
||||
</ul>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th><th>Description</th>
|
||||
</tr>
|
||||
<tr><td>ProcessDemo</td><td>Executing an external application</td></tr>
|
||||
<tr><td>ThreadsDemo</td><td>Two ways of creating threads</td></tr>
|
||||
</table>
|
||||
|
||||
<h3>04 - Concurrency - Synchronization and Deadlock</h3>
|
||||
<ul>
|
||||
<li>SemaphoreDemo</li>
|
||||
</ul>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th><th>Description</th>
|
||||
</tr>
|
||||
<tr><td>ProConUnprotected</td><td>Unprotected concurrency</td></tr>
|
||||
<tr><td>SemaphoreDemo</td><td></td></tr>
|
||||
</table>
|
||||
|
||||
<h3>05 - Concurrency - High-Level Concurrency Primitives</h3>
|
||||
<ul>
|
||||
<li>MonitorCase1</li>
|
||||
<li>MonitorCase2</li>
|
||||
<li>CircularBuffer</li>
|
||||
<li>ConditionDemo</li>
|
||||
</ul>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th><th>Description</th>
|
||||
</tr>
|
||||
<tr><td>CircularBuffer</td><td>The principle</td></tr>
|
||||
<tr><td>ConditionDemo</td><td>Circular buffer using Condition Variables</td></tr>
|
||||
<tr><td>MonitorCase1</td><td>Why wont it work?</td></tr>
|
||||
<tr><td>MonitorCase2</td><td>The solution</td></tr>
|
||||
</table>
|
||||
|
||||
<h3>07 - IO - Blocking vs non-blocking</h3>
|
||||
<ul>
|
||||
<li>ClassicIoUnbuffered</li>
|
||||
<li>ClassicIoBuffered</li>
|
||||
<li>ClassicIoManBuffered</li>
|
||||
<li>ClassicIoWholeFile</li>
|
||||
<li>NioLineCount</li>
|
||||
</ul>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th><th>Description</th>
|
||||
</tr>
|
||||
<tr><td>ClassicIoUnbuffered</td><td></td></tr>
|
||||
<tr><td>ClassicIoBuffered</td><td></td></tr>
|
||||
<tr><td>ClassicIoManBuffered</td><td></td></tr>
|
||||
<tr><td>ClassicIoWholeFile</td><td></td></tr>
|
||||
<tr><td>NioLineCount</td><td></td></tr>
|
||||
</table>
|
||||
|
||||
<h3>09 - Scheduling</h3>
|
||||
<p>As a treat: A small game taken from: https://zetcode.com/javagames/movingsprites/
|
||||
<ul>
|
||||
<li>Game-1: Simple moving spaceship</li>
|
||||
<li>Game-2: Added missile and refactoring</li>
|
||||
</ul>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Name</th><th>Description</th>
|
||||
</tr>
|
||||
<tr><td>Game-1</td><td>Simple moving spaceship</td></tr>
|
||||
<tr><td>Game-2</td><td>Added missile and refactoring</td></tr>
|
||||
<tr><td>Game-3</td><td>Missiles threaded</td></tr>
|
||||
</table>
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ public class ClassicIoBuffered {
|
||||
int lines = lineCount("input.txt", true);
|
||||
long stopTime = System.nanoTime();
|
||||
System.out.println("Lines: " + String.valueOf(lines));
|
||||
System.out.println("Time it took: " + (stopTime - startTime - dryRun) + " ");
|
||||
System.out.println("Time to complete: " + (stopTime - startTime - dryRun) + " " + dryRun);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class ClassicIoManBuffered {
|
||||
int lines = lineCount("input.txt", true);
|
||||
long stopTime = System.nanoTime();
|
||||
System.out.println("Lines: " + String.valueOf(lines));
|
||||
System.out.println("Time it took: " + (stopTime - startTime - dryRun) + " ");
|
||||
System.out.println("Time to complete: " + (stopTime - startTime - dryRun) + " " + dryRun);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class ClassicIoUnbuffered {
|
||||
int lines = lineCount("input.txt", true);
|
||||
long stopTime = System.nanoTime();
|
||||
System.out.println("Lines: " + String.valueOf(lines));
|
||||
System.out.println("Time it took: " + (stopTime - startTime - dryRun) + " " + dryRun);
|
||||
System.out.println("Time to complete: " + (stopTime - startTime - dryRun) + " " + dryRun);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class ClassicIoWholeFile {
|
||||
int lines = lineCount("input.txt", true);
|
||||
long stopTime = System.nanoTime();
|
||||
System.out.println("Lines: " + String.valueOf(lines));
|
||||
System.out.println("Time it took: " + (stopTime - startTime - dryRun) + " ");
|
||||
System.out.println("Time to complete: " + (stopTime - startTime - dryRun) + " " + dryRun);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
2
Technology/Game-1/bin/.gitignore
vendored
Normal file
2
Technology/Game-1/bin/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/Game/
|
||||
/resources/
|
||||
2
Technology/Game-2/bin/.gitignore
vendored
Normal file
2
Technology/Game-2/bin/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/Game/
|
||||
/resources/
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 9.7 KiB After Width: | Height: | Size: 269 B |
Binary file not shown.
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 738 B |
@@ -2,6 +2,7 @@
|
||||
|
||||
package Game;
|
||||
|
||||
//Inherit some methods shared with class SpaceShip
|
||||
public class Missile extends Sprite {
|
||||
|
||||
private final int BOARD_WIDTH = 800;
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
// Inherit some methods shared with class Missile
|
||||
public class SpaceShip extends Sprite {
|
||||
|
||||
private int dx;
|
||||
@@ -19,7 +20,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
private void initSpaceShip() {
|
||||
|
||||
missiles = new ArrayList<>();
|
||||
|
||||
loadImage("src/resources/spaceship.png");
|
||||
@@ -36,7 +36,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_SPACE) {
|
||||
@@ -65,7 +64,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_LEFT) {
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
// Origin: https://zetcode.com/javagames/movingsprites/
|
||||
|
||||
// SpaceShip and Missile have some methods in common.
|
||||
// We can just as well have them inherit those methods
|
||||
|
||||
// The protected modifier specifies that the member can only be accessed within its own package
|
||||
// The private modifier specifies that the member can only be accessed within its own class.
|
||||
|
||||
package Game;
|
||||
|
||||
import java.awt.Image;
|
||||
@@ -14,21 +20,19 @@ public class Sprite {
|
||||
protected boolean visible;
|
||||
protected Image image;
|
||||
|
||||
// Set the initial position
|
||||
public Sprite(int x, int y) {
|
||||
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
visible = true;
|
||||
}
|
||||
|
||||
protected void loadImage(String imageName) {
|
||||
|
||||
ImageIcon ii = new ImageIcon(imageName);
|
||||
image = ii.getImage();
|
||||
}
|
||||
|
||||
protected void getImageDimensions() {
|
||||
|
||||
width = image.getWidth(null);
|
||||
height = image.getHeight(null);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
package Game;
|
||||
|
||||
//Inherit some methods shared with class SpaceShip
|
||||
public class Missile extends Sprite {
|
||||
|
||||
private final int BOARD_WIDTH = 800;
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
//Inherit some methods shared with class Missile
|
||||
public class SpaceShip extends Sprite {
|
||||
|
||||
private int dx;
|
||||
@@ -19,7 +20,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
private void initSpaceShip() {
|
||||
|
||||
missiles = new ArrayList<>();
|
||||
|
||||
loadImage("src/resources/spaceship.png");
|
||||
@@ -36,7 +36,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_SPACE) {
|
||||
@@ -65,7 +64,6 @@ public class SpaceShip extends Sprite {
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
int key = e.getKeyCode();
|
||||
|
||||
if (key == KeyEvent.VK_LEFT) {
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
// Origin: https://zetcode.com/javagames/movingsprites/
|
||||
|
||||
// SpaceShip and Missile have some methods in common.
|
||||
// We can just as well have them inherit those methods
|
||||
|
||||
// The protected modifier specifies that the member can only be accessed within its own package
|
||||
// The private modifier specifies that the member can only be accessed within its own class.
|
||||
|
||||
package Game;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
@@ -87,7 +87,7 @@ public class NioLineCount {
|
||||
int lines = lineCount("input.txt", true);
|
||||
long stopTime = System.nanoTime();
|
||||
System.out.println("Lines: " + String.valueOf(lines));
|
||||
System.out.println("Time it took: " + (stopTime - startTime - dryRun) + " " + dryRun);
|
||||
System.out.println("Time to complete: " + (stopTime - startTime - dryRun) + " " + dryRun);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
10
Technology/ProConUnprotected/.classpath
Normal file
10
Technology/ProConUnprotected/.classpath
Normal file
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Technology/ProConUnprotected/.project
Normal file
17
Technology/ProConUnprotected/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>ProConUnprotected</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=21
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||
org.eclipse.jdt.core.compiler.release=enabled
|
||||
org.eclipse.jdt.core.compiler.source=21
|
||||
5
Technology/ProConUnprotected/src/Buffer.java
Normal file
5
Technology/ProConUnprotected/src/Buffer.java
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
public interface Buffer {
|
||||
public void set( int va1ue ); // place value into Buffer
|
||||
public int get(); // return value from Buffer
|
||||
}
|
||||
24
Technology/ProConUnprotected/src/Consumer.java
Normal file
24
Technology/ProConUnprotected/src/Consumer.java
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
public class Consumer extends Thread {
|
||||
private Buffer sharedLocation;
|
||||
|
||||
public Consumer( Buffer shared ) {
|
||||
super("Consumer");
|
||||
sharedLocation = shared;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
int sum = 0;
|
||||
for (int count = 1; count <= 4; count++) {
|
||||
try {
|
||||
Thread.sleep((int)(Math.random() * 3001));
|
||||
sum += sharedLocation.get();
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.err.println( getName() + " read values totalling: " + sum + " ...DONE");
|
||||
}
|
||||
|
||||
}
|
||||
22
Technology/ProConUnprotected/src/Producer.java
Normal file
22
Technology/ProConUnprotected/src/Producer.java
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
public class Producer extends Thread {
|
||||
private Buffer sharedLocation;
|
||||
|
||||
public Producer( Buffer shared ) {
|
||||
super("Producer");
|
||||
sharedLocation = shared;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
for (int count = 1; count <= 4; count++) {
|
||||
try {
|
||||
Thread.sleep((int)(Math.random() * 3001));
|
||||
sharedLocation.set(count);
|
||||
}
|
||||
catch (InterruptedException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
System.err.println( getName() + " done producing.");
|
||||
}
|
||||
}
|
||||
15
Technology/ProConUnprotected/src/SharedBufferTest.java
Normal file
15
Technology/ProConUnprotected/src/SharedBufferTest.java
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
public class SharedBufferTest {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO Auto-generated method stub
|
||||
Buffer sharedLocation = new UnsynchronizedBuffer();
|
||||
|
||||
Producer producer = new Producer( sharedLocation);
|
||||
Consumer consumer = new Consumer( sharedLocation);
|
||||
|
||||
producer.start();
|
||||
consumer.start();
|
||||
}
|
||||
|
||||
}
|
||||
15
Technology/ProConUnprotected/src/UnsynchronizedBuffer.java
Normal file
15
Technology/ProConUnprotected/src/UnsynchronizedBuffer.java
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
public class UnsynchronizedBuffer implements Buffer {
|
||||
private int buffer = -1;
|
||||
|
||||
public void set( int value ) {
|
||||
System.err.println( Thread.currentThread().getName() + " writes " + value);
|
||||
buffer = value;
|
||||
}
|
||||
|
||||
public int get() {
|
||||
System.err.println( Thread.currentThread().getName() + " reads " + buffer);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,7 +28,10 @@ class MyThread extends Thread
|
||||
String threadName;
|
||||
|
||||
public MyThread(Semaphore mySemPro, Semaphore mySemCon, String threadName)
|
||||
{
|
||||
{
|
||||
// The Thread class constructor that takes a String argument creates
|
||||
// a Thread with a specific name
|
||||
// (as opposed to automatically generated names such as Thread-1).
|
||||
super(threadName);
|
||||
// We set the semaphores from the main method
|
||||
this.semPro = mySemPro;
|
||||
|
||||
Reference in New Issue
Block a user