removed custom name for threads - using internal name instead

This commit is contained in:
Sebastian Brosch 2024-05-30 11:37:17 +02:00
parent dad420e8d8
commit 108cd3259e
2 changed files with 6 additions and 10 deletions

View File

@ -8,8 +8,8 @@ package VL14.Aufgabe01;
class Aufgabe01 {
public static void main(String[] args) {
Counter counterUp = new Counter("T1", 1, 100);
Counter counterDown = new Counter("T2", -10);
Counter counterUp = new Counter(1, 100);
Counter counterDown = new Counter(-10);
Thread threadCounterUp = new Thread(counterUp);
Thread threadCounterDown = new Thread(counterDown);
threadCounterUp.start();

View File

@ -10,29 +10,25 @@ public class Counter implements Runnable {
// 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);
public Counter(int number, int start) {
this(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) {
public Counter(int number) {
this.number = number;
this.name = name;
}
/**
@ -50,7 +46,7 @@ public class Counter implements Runnable {
private void count() {
synchronized (getClass()) {
counter += this.number;
System.out.printf("%3d [%s: %3d]\n", counter, this.name, this.number);
System.out.printf("%3d [%s: %3d]\n", counter, Thread.currentThread().getName(), this.number);
}
}
}