Compare commits

..

10 Commits

Author SHA1 Message Date
Karsten Jeppesen
e3c1fa0f12 Update README.md 2025-03-31 07:27:11 +02:00
717dd7835e ProConUnprotected: Initial commit 2025-03-19 20:42:54 +01:00
Karsten Jeppesen
519152a7b9 Update README.md 2025-03-19 20:41:51 +01:00
8c12a1a904 SemaphoreDemo: Added comments 2025-03-19 20:00:56 +01:00
ab6ad3dcc7 git ignores 2025-03-19 19:45:57 +01:00
Karsten Jeppesen
c62e6a5aa1 Update README.md 2024-04-25 08:05:01 +02:00
Karsten Jeppesen
dad6903407 Update README.md 2024-04-23 14:15:49 +02:00
12706d9ec3 Merge branch 'master' of github.com:UCNProf/KAJE-JAVA 2024-04-23 14:13:57 +02:00
0334a99776 Game-1,2,3: Added comments 2024-04-23 14:13:18 +02:00
661c10c2b1 IO: Uniform report syntax 2024-04-03 07:23:32 +02:00
26 changed files with 196 additions and 39 deletions

View File

@@ -2,33 +2,54 @@
All things Java<br/> All things Java<br/>
<h3>Session 03 - Concurrency - Processes and Threads</h3> <h3>Session 03 - Concurrency - Processes and Threads</h3>
<ul> <table>
<li>ProcessDemo</li> <tr>
<li>ThreadsDemo</li> <th>Name</th><th>Description</th>
</ul> </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> <h3>04 - Concurrency - Synchronization and Deadlock</h3>
<ul> <table>
<li>SemaphoreDemo</li> <tr>
</ul> <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> <h3>05 - Concurrency - High-Level Concurrency Primitives</h3>
<ul> <table>
<li>MonitorCase1</li> <tr>
<li>MonitorCase2</li> <th>Name</th><th>Description</th>
<li>CircularBuffer</li> </tr>
<li>ConditionDemo</li> <tr><td>CircularBuffer</td><td>The principle</td></tr>
</ul> <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> <h3>07 - IO - Blocking vs non-blocking</h3>
<ul> <table>
<li>ClassicIoUnbuffered</li> <tr>
<li>ClassicIoBuffered</li> <th>Name</th><th>Description</th>
<li>ClassicIoManBuffered</li> </tr>
<li>ClassicIoWholeFile</li> <tr><td>ClassicIoUnbuffered</td><td></td></tr>
<li>NioLineCount</li> <tr><td>ClassicIoBuffered</td><td></td></tr>
</ul> <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> <h3>09 - Scheduling</h3>
<p>As a treat: A small game taken from: https://zetcode.com/javagames/movingsprites/ <p>As a treat: A small game taken from: https://zetcode.com/javagames/movingsprites/
<ul> <table>
<li>Game-1: Simple moving spaceship</li> <tr>
<li>Game-2: Added missile and refactoring</li> <th>Name</th><th>Description</th>
</ul> </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>

View File

@@ -48,7 +48,7 @@ public class ClassicIoBuffered {
int lines = lineCount("input.txt", true); int lines = lineCount("input.txt", true);
long stopTime = System.nanoTime(); long stopTime = System.nanoTime();
System.out.println("Lines: " + String.valueOf(lines)); 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);
} }
} }

View File

@@ -43,7 +43,7 @@ public class ClassicIoManBuffered {
int lines = lineCount("input.txt", true); int lines = lineCount("input.txt", true);
long stopTime = System.nanoTime(); long stopTime = System.nanoTime();
System.out.println("Lines: " + String.valueOf(lines)); 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);
} }
} }

View File

@@ -40,7 +40,7 @@ public class ClassicIoUnbuffered {
int lines = lineCount("input.txt", true); int lines = lineCount("input.txt", true);
long stopTime = System.nanoTime(); long stopTime = System.nanoTime();
System.out.println("Lines: " + String.valueOf(lines)); 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);
} }
} }

View File

@@ -46,7 +46,7 @@ public class ClassicIoWholeFile {
int lines = lineCount("input.txt", true); int lines = lineCount("input.txt", true);
long stopTime = System.nanoTime(); long stopTime = System.nanoTime();
System.out.println("Lines: " + String.valueOf(lines)); 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
View File

@@ -0,0 +1,2 @@
/Game/
/resources/

2
Technology/Game-2/bin/.gitignore vendored Normal file
View 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

View File

@@ -2,6 +2,7 @@
package Game; package Game;
//Inherit some methods shared with class SpaceShip
public class Missile extends Sprite { public class Missile extends Sprite {
private final int BOARD_WIDTH = 800; private final int BOARD_WIDTH = 800;

View File

@@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
// Inherit some methods shared with class Missile
public class SpaceShip extends Sprite { public class SpaceShip extends Sprite {
private int dx; private int dx;
@@ -19,7 +20,6 @@ public class SpaceShip extends Sprite {
} }
private void initSpaceShip() { private void initSpaceShip() {
missiles = new ArrayList<>(); missiles = new ArrayList<>();
loadImage("src/resources/spaceship.png"); loadImage("src/resources/spaceship.png");
@@ -36,7 +36,6 @@ public class SpaceShip extends Sprite {
} }
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
int key = e.getKeyCode(); int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) { if (key == KeyEvent.VK_SPACE) {
@@ -65,7 +64,6 @@ public class SpaceShip extends Sprite {
} }
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
int key = e.getKeyCode(); int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) { if (key == KeyEvent.VK_LEFT) {

View File

@@ -1,5 +1,11 @@
// Origin: https://zetcode.com/javagames/movingsprites/ // 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; package Game;
import java.awt.Image; import java.awt.Image;
@@ -14,21 +20,19 @@ public class Sprite {
protected boolean visible; protected boolean visible;
protected Image image; protected Image image;
// Set the initial position
public Sprite(int x, int y) { public Sprite(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
visible = true; visible = true;
} }
protected void loadImage(String imageName) { protected void loadImage(String imageName) {
ImageIcon ii = new ImageIcon(imageName); ImageIcon ii = new ImageIcon(imageName);
image = ii.getImage(); image = ii.getImage();
} }
protected void getImageDimensions() { protected void getImageDimensions() {
width = image.getWidth(null); width = image.getWidth(null);
height = image.getHeight(null); height = image.getHeight(null);
} }

View File

@@ -2,6 +2,7 @@
package Game; package Game;
//Inherit some methods shared with class SpaceShip
public class Missile extends Sprite { public class Missile extends Sprite {
private final int BOARD_WIDTH = 800; private final int BOARD_WIDTH = 800;

View File

@@ -6,6 +6,7 @@ import java.awt.event.KeyEvent;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
//Inherit some methods shared with class Missile
public class SpaceShip extends Sprite { public class SpaceShip extends Sprite {
private int dx; private int dx;
@@ -19,7 +20,6 @@ public class SpaceShip extends Sprite {
} }
private void initSpaceShip() { private void initSpaceShip() {
missiles = new ArrayList<>(); missiles = new ArrayList<>();
loadImage("src/resources/spaceship.png"); loadImage("src/resources/spaceship.png");
@@ -36,7 +36,6 @@ public class SpaceShip extends Sprite {
} }
public void keyPressed(KeyEvent e) { public void keyPressed(KeyEvent e) {
int key = e.getKeyCode(); int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) { if (key == KeyEvent.VK_SPACE) {
@@ -65,7 +64,6 @@ public class SpaceShip extends Sprite {
} }
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
int key = e.getKeyCode(); int key = e.getKeyCode();
if (key == KeyEvent.VK_LEFT) { if (key == KeyEvent.VK_LEFT) {

View File

@@ -1,5 +1,11 @@
// Origin: https://zetcode.com/javagames/movingsprites/ // 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; package Game;
import java.awt.Image; import java.awt.Image;

View File

@@ -87,7 +87,7 @@ public class NioLineCount {
int lines = lineCount("input.txt", true); int lines = lineCount("input.txt", true);
long stopTime = System.nanoTime(); long stopTime = System.nanoTime();
System.out.println("Lines: " + String.valueOf(lines)); 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);
} }
} }

View 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>

View 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>

View File

@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8

View File

@@ -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

View File

@@ -0,0 +1,5 @@
public interface Buffer {
public void set( int va1ue ); // place value into Buffer
public int get(); // return value from Buffer
}

View 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");
}
}

View 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.");
}
}

View 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();
}
}

View 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;
}
}

View File

@@ -29,6 +29,9 @@ class MyThread extends Thread
public MyThread(Semaphore mySemPro, Semaphore mySemCon, 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); super(threadName);
// We set the semaphores from the main method // We set the semaphores from the main method
this.semPro = mySemPro; this.semPro = mySemPro;