added ex S1

This commit is contained in:
2025-01-06 16:05:48 +01:00
parent a7216b1642
commit dbd83223fe
3 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package exercises.ex_s1;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
public class Farmer implements Runnable {
private String name;
private ConcurrentHashMap<String, Integer> stalls;
private final Random random = new Random();
public Farmer(String name, ConcurrentHashMap<String, Integer> stalls) {
this.name = name;
this.stalls = stalls;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep((long) (1000L + Math.random() * 2000L));
} catch (InterruptedException e) {
break;
}
String key = "box" + random.nextInt(1, 4);
int units = random.nextInt(10, 100);
stalls.put(key, units);
System.out.println(name + " puts " + units + " units of food in " + key);
}
}
}