2024-05-30 09:27:24 +00:00
|
|
|
package VL14.Aufgabe02;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class which represents a user.
|
|
|
|
*
|
|
|
|
* @author Sebastian Brosch
|
|
|
|
*/
|
|
|
|
public class User implements Runnable {
|
|
|
|
private int number = 0;
|
2024-06-03 20:04:50 +00:00
|
|
|
private int wait = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor to initialize a User.
|
|
|
|
*/
|
|
|
|
public User() {
|
|
|
|
this.wait = 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor to initialize a User.
|
|
|
|
*
|
|
|
|
* @param wait The time the user is waiting for creating new print jobs.
|
|
|
|
*/
|
|
|
|
public User(int wait) {
|
|
|
|
this.wait = wait;
|
|
|
|
}
|
2024-05-30 09:27:24 +00:00
|
|
|
|
|
|
|
public void run() {
|
|
|
|
while (true) {
|
|
|
|
synchronized (PrinterQueue.queue) {
|
2024-06-03 20:04:50 +00:00
|
|
|
int data = this.number++;
|
|
|
|
PrinterQueue.queue.add(data);
|
|
|
|
System.out.println("User " + Thread.currentThread().threadId() + " adds: " + data);
|
|
|
|
PrinterQueue.queue.notifyAll();
|
2024-05-30 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2024-06-03 20:04:50 +00:00
|
|
|
Thread.sleep(this.wait);
|
2024-05-30 09:27:24 +00:00
|
|
|
} catch (InterruptedException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|