Hey Leute,
leider schaffe ich es nicht die run() Methode in Wasser so zu ändern, dass der Name des aufrufenden Threads in den String gepackt werden kann... Danke für eure Ideen.
package de.codegym.task.task16.task1620;
import java.util.ArrayList;
import java.util.List;
/*
Einer für alle, alle für einen
*/
public class Solution {
static boolean b = true;
public static byte threadCount = 3;
static List<Thread> threads = new ArrayList<>(threadCount);
public static void main(String[] args) throws InterruptedException {
initThreadsAndStart();
Thread.sleep(3000);
ourInterrupt();
}
public static void ourInterrupt() {
//schreib hier deinen Code
b = false;
}
private static void initThreadsAndStart() {
Water water = new Water("Wasser");
for (int i = 0; i < threadCount; i++) {
threads.add(new Thread(water, "#" + i));
}
for (int i = 0; i < threadCount; i++) {
threads.get(i).start();
}
}
public static class Water implements Runnable {
private String sharedResource;
public Water(String sharedResource) {
this.sharedResource = sharedResource;
}
public void run() {
//korrigiere 2 Variablen
boolean isCurrentThreadInterrupted = false;
String threadName = super.Thread.getName();
try {
while (!isCurrentThreadInterrupted&&b==true) {
System.out.println("Objekt " + sharedResource + ", Thread " + threadName);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
}
}
}
}