Initial commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Gradle build configuration for specific lab module / exercise
|
||||
*/
|
||||
// the Java plugin is added by default in the main lab configuration
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
// Project/Module information
|
||||
description = 'Lab01 Printer Solution'
|
||||
group = 'ch.zhaw.prog2'
|
||||
version = '2022.1'
|
||||
|
||||
// Dependency configuration
|
||||
// repositories to download dependencies from
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
// required dependencies
|
||||
dependencies {
|
||||
|
||||
}
|
||||
|
||||
// Java plugin configuration
|
||||
java {
|
||||
// By default the Java version of the gradle process is used as source/target version.
|
||||
// This can be overridden, to ensure a specific version. Enable only if required.
|
||||
// sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
|
||||
// targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
|
||||
|
||||
// Java compiler specific options
|
||||
compileJava {
|
||||
// source files should be UTF-8 encoded
|
||||
options.encoding = 'UTF-8'
|
||||
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package ch.zhaw.prog2.printer;
|
||||
|
||||
public class Printer {
|
||||
|
||||
// test program
|
||||
public static void main(String[] arg) {
|
||||
PrinterThread a = new PrinterThread("PrinterA", '.', 10);
|
||||
PrinterThread b = new PrinterThread("PrinterB", '*', 20);
|
||||
a.start();
|
||||
b.start();
|
||||
b.run(); // wie kann das abgefangen werden?
|
||||
}
|
||||
|
||||
|
||||
private static class PrinterThread extends Thread {
|
||||
char symbol;
|
||||
int sleepTime;
|
||||
|
||||
public PrinterThread(String name, char symbol, int sleepTime) {
|
||||
super(name);
|
||||
this.symbol = symbol;
|
||||
this.sleepTime = sleepTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(getName() + " run started...");
|
||||
for (int i = 1; i < 100; i++) {
|
||||
System.out.print(symbol);
|
||||
try {
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
System.out.println('\n' + getName() + " run ended.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package ch.zhaw.prog2.printer;
|
||||
|
||||
|
||||
public class PrinterLsgA {
|
||||
|
||||
// test program
|
||||
public static void main(String[] arg) {
|
||||
PrinterThread a = new PrinterThread("PrinterA", '.', 0);
|
||||
PrinterThread b = new PrinterThread("PrinterB", '*', 0);
|
||||
a.start();
|
||||
b.start();
|
||||
b.run(); // wie kann das abgefangen werden?
|
||||
}
|
||||
|
||||
|
||||
private static class PrinterThread extends Thread {
|
||||
char symbol;
|
||||
int sleepTime;
|
||||
|
||||
public PrinterThread(String name, char symbol, int sleepTime) {
|
||||
super(name);
|
||||
this.symbol = symbol;
|
||||
this.sleepTime = sleepTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (this != currentThread()) {
|
||||
throw new IllegalStateException("run() must not be called directly");
|
||||
}
|
||||
System.out.println(getName() + " run started...");
|
||||
for (int i = 1; i < 100; i++) {
|
||||
System.out.print(symbol);
|
||||
try {
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
System.out.println('\n' + getName() + " run ended.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package ch.zhaw.prog2.printer;
|
||||
|
||||
|
||||
public class PrinterLsgB {
|
||||
|
||||
// test program
|
||||
public static void main(String[] arg) {
|
||||
PrinterRunnable a = new PrinterRunnable('.', 0);
|
||||
PrinterRunnable b = new PrinterRunnable('*', 0);
|
||||
Thread t1 = new Thread(a, "PrinterA");
|
||||
Thread t2 = new Thread(b, "PrinterB");
|
||||
t1.start();
|
||||
t2.start();
|
||||
}
|
||||
|
||||
private static class PrinterRunnable implements Runnable {
|
||||
char symbol;
|
||||
int sleepTime;
|
||||
|
||||
public PrinterRunnable(char symbol, int sleepTime) {
|
||||
this.symbol = symbol;
|
||||
this.sleepTime = sleepTime;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Thread current = Thread.currentThread();
|
||||
System.out.println(current.getName() + " run started...");
|
||||
for (int i = 1; i < 100; i++) {
|
||||
System.out.print(symbol);
|
||||
try {
|
||||
Thread.sleep(sleepTime);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
System.out.println('\n' + current.getName() + " run ended.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package ch.zhaw.prog2.printer;
|
||||
|
||||
|
||||
public class PrinterLsgCD {
|
||||
|
||||
// test program
|
||||
public static void main(String[] arg) {
|
||||
PrinterRunnable a = new PrinterRunnable('.', 0); // different if you change the sleep
|
||||
// time?
|
||||
PrinterRunnable b = new PrinterRunnable('*', 0); // different if you change the sleep
|
||||
// time?
|
||||
Thread t1 = new Thread(a, "PrinterA");
|
||||
Thread t2 = new Thread(b, "PrinterB");
|
||||
t1.start();
|
||||
t2.start();
|
||||
// d)
|
||||
try {
|
||||
t1.join(); // wait for t1 to die
|
||||
t2.join(); // wait for t2 to die
|
||||
} catch (InterruptedException e) {
|
||||
System.err.println("Join interrupted");
|
||||
}
|
||||
}
|
||||
|
||||
private static class PrinterRunnable implements Runnable {
|
||||
char symbol;
|
||||
int sleepTime;
|
||||
|
||||
public PrinterRunnable(char symbol, int sleepTime) {
|
||||
this.symbol = symbol;
|
||||
this.sleepTime = sleepTime;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Thread current = Thread.currentThread();
|
||||
System.out.println(current.getName() + " run started...");
|
||||
for (int i = 1; i < 100; i++) {
|
||||
System.out.print(symbol);
|
||||
// c)
|
||||
Thread.yield(); // release CPU and give other thread a chance
|
||||
// try {
|
||||
// Thread.sleep(sleepTime);
|
||||
// } catch (InterruptedException e) {
|
||||
// System.out.println(e.getMessage());
|
||||
// }
|
||||
}
|
||||
System.out.println('\n' + current.getName() + " run ended.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user