Stworzenie 3 wątków za pomocą wyrażenia lambda wypisujących liczby od 0 do 100. 1 wątek liczby podzielne przez 3, 2 wątek liczby z resztą 1 i 3 wątek liczby z resztą 2. Nie mam pojęcia jak stworzyć te wątki lambdą żeby program działał poprawnie.
public class Main {
public static void main(String[] args) throws InterruptedException {
Counter c0 = new Counter(0);
Counter c1 = new Counter(1);
Counter c2 = new Counter(2);
Thread t0 = new Thread(c0);
Thread t1 = new Thread(c1);
Thread t2 = new Thread(c2);
t0.start();
t1.start();
t2.start();
t0.join();
t1.join();
t2.join();
System.out.printf("END");
}
}
public class Counter implements Runnable {
final private int id;
public static int c = 0;
private static Object mutex = new Object();
public Counter(int id) {
this.id = id;
}
@Override
public void run() {
while(c < 100) {
synchronized (mutex) {
while (c % 3 != id) {
try {
mutex.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(c <= 100) {
System.out.println(id + " -> " + c);
c++;
mutex.notifyAll();
}
}
}
}
}