Compare commits
3 Commits
9506ee39ed
...
dad420e8d8
Author | SHA1 | Date | |
---|---|---|---|
dad420e8d8 | |||
c08f4d3196 | |||
b96c8c7b21 |
@ -6,6 +6,5 @@ Lösungen zu den Aufgaben der Vorlesung "Programmieren" des ersten und zweiten S
|
|||||||
- **Editor**: [Visual Studio Code](https://code.visualstudio.com/)
|
- **Editor**: [Visual Studio Code](https://code.visualstudio.com/)
|
||||||
- [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack)
|
- [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack)
|
||||||
- [EditorConfig for VS Code](https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig)
|
- [EditorConfig for VS Code](https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig)
|
||||||
- [Rainbow Brackets](https://marketplace.visualstudio.com/items?itemName=tal7aouy.rainbow-bracket)
|
|
||||||
- **Java**: [Java Development Kit 21 (LTS)](https://www.oracle.com/de/java/technologies/downloads/#java21)
|
- **Java**: [Java Development Kit 21 (LTS)](https://www.oracle.com/de/java/technologies/downloads/#java21)
|
||||||
- **Style Guide**: [Google Java Style Guide](https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml)
|
- **Style Guide**: [Google Java Style Guide](https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml)
|
18
VL14/Aufgabe01/Aufgabe01.java
Normal file
18
VL14/Aufgabe01/Aufgabe01.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package VL14.Aufgabe01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vorlesung 14 / Aufgabe 1
|
||||||
|
*
|
||||||
|
* @author Sebastian Brosch
|
||||||
|
*/
|
||||||
|
class Aufgabe01 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Counter counterUp = new Counter("T1", 1, 100);
|
||||||
|
Counter counterDown = new Counter("T2", -10);
|
||||||
|
Thread threadCounterUp = new Thread(counterUp);
|
||||||
|
Thread threadCounterDown = new Thread(counterDown);
|
||||||
|
threadCounterUp.start();
|
||||||
|
threadCounterDown.start();
|
||||||
|
}
|
||||||
|
}
|
56
VL14/Aufgabe01/Counter.java
Normal file
56
VL14/Aufgabe01/Counter.java
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package VL14.Aufgabe01;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class representing a counter.
|
||||||
|
*
|
||||||
|
* @author Sebastian Brosch
|
||||||
|
*/
|
||||||
|
public class Counter implements Runnable {
|
||||||
|
static int counter;
|
||||||
|
|
||||||
|
// some information of the counter.
|
||||||
|
private int number;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new counter with initializing the start value.
|
||||||
|
*
|
||||||
|
* @param name The name of the counter.
|
||||||
|
* @param number The number to increment or decrement the counter.
|
||||||
|
* @param start The start value of the counter.
|
||||||
|
*/
|
||||||
|
public Counter(String name, int number, int start) {
|
||||||
|
this(name, number);
|
||||||
|
counter = start;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new counter without initializing the start value.
|
||||||
|
*
|
||||||
|
* @param name The name of the counter.
|
||||||
|
* @param number The number to increment or decrement the counter.
|
||||||
|
*/
|
||||||
|
public Counter(String name, int number) {
|
||||||
|
this.number = number;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to run some code in a Thread.
|
||||||
|
*/
|
||||||
|
public void run() {
|
||||||
|
while (counter > 0 && counter < 1000) {
|
||||||
|
this.count();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to increment or decrement the counter.
|
||||||
|
*/
|
||||||
|
private void count() {
|
||||||
|
synchronized (getClass()) {
|
||||||
|
counter += this.number;
|
||||||
|
System.out.printf("%3d [%s: %3d]\n", counter, this.name, this.number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
VL14/Aufgabe02/Aufgabe02.java
Normal file
27
VL14/Aufgabe02/Aufgabe02.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package VL14.Aufgabe02;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vorlesung 14 / Aufgabe 2
|
||||||
|
*
|
||||||
|
* @author Sebastian Brosch
|
||||||
|
*/
|
||||||
|
public class Aufgabe02 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final int NUMBER_OF_USERS = 5;
|
||||||
|
final int NUMBER_OF_PRINTERS = 2;
|
||||||
|
|
||||||
|
// create some users.
|
||||||
|
for (int u = 0; u < NUMBER_OF_USERS; u++) {
|
||||||
|
User user = new User();
|
||||||
|
Thread threadUser = new Thread(user);
|
||||||
|
threadUser.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// create some printers.
|
||||||
|
for (int p = 0; p < NUMBER_OF_PRINTERS; p++) {
|
||||||
|
Printer printer = new Printer();
|
||||||
|
Thread threadPrinter = new Thread(printer);
|
||||||
|
threadPrinter.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
VL14/Aufgabe02/Printer.java
Normal file
23
VL14/Aufgabe02/Printer.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package VL14.Aufgabe02;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which represents a printer.
|
||||||
|
*
|
||||||
|
* @author Sebastian Brosch
|
||||||
|
*/
|
||||||
|
public class Printer implements Runnable {
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
synchronized (PrinterQueue.queue) {
|
||||||
|
if (PrinterQueue.queue.size() > 0) {
|
||||||
|
System.out.println(PrinterQueue.queue.poll());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(500);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
VL14/Aufgabe02/PrinterQueue.java
Normal file
12
VL14/Aufgabe02/PrinterQueue.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package VL14.Aufgabe02;
|
||||||
|
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which represents a printer queue.
|
||||||
|
*
|
||||||
|
* @author Sebastian Brosch
|
||||||
|
*/
|
||||||
|
public class PrinterQueue {
|
||||||
|
static LinkedList<String> queue = new LinkedList<String>();
|
||||||
|
}
|
23
VL14/Aufgabe02/User.java
Normal file
23
VL14/Aufgabe02/User.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package VL14.Aufgabe02;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which represents a user.
|
||||||
|
*
|
||||||
|
* @author Sebastian Brosch
|
||||||
|
*/
|
||||||
|
public class User implements Runnable {
|
||||||
|
private int number = 0;
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
synchronized (PrinterQueue.queue) {
|
||||||
|
PrinterQueue.queue.add(Thread.currentThread().getName() + ": " + this.number++);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user