ProConUnprotected: Initial commit
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user