VL-Programmieren/VL14/Aufgabe01/Counter.java

53 lines
1.1 KiB
Java

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;
/**
* Create a new counter with initializing the start value.
*
* @param number The number to increment or decrement the counter.
* @param start The start value of the counter.
*/
public Counter(int number, int start) {
this(number);
counter = start;
}
/**
* Create a new counter without initializing the start value.
*
* @param number The number to increment or decrement the counter.
*/
public Counter(int number) {
this.number = number;
}
/**
* 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, Thread.currentThread().getName(), this.number);
}
}
}