I'm learning Java and I've started writing some tasks. Could you give me feedback for it?
Write a static method
andThen
that takes as parameters two Runnable instances and returns a Runnable that runs the first, then the second. In the main method, pass two lambda expressions into a call toandThen
, and run the returned instance.
public class Exercise7 {
public static class Thread1 implements Runnable {
@Override
public void run() {
System.out.println("Echo from thread1");
}
}
public static class Thread2 implements Runnable {
@Override
public void run() {
System.out.println("Echo from thread2");
}
}
public static Runnable andThen(Runnable thread1, Runnable thread2) {
return () -> {
thread1.run();
thread2.run();
};
}
public static void main(String[] args) {
Runnable thread1 = new Thread1();
Runnable thread2 = new Thread2();
andThen(thread1,thread2).run();
}
}
thread1
andthread2
are "threads" in name only. There is no multithreading going on: all of your code is running on the main thread. \$\endgroup\$