public class Solution {
public static class YieldRunnable implements Runnable {
private int index;
public YieldRunnable(int index) {
this.index = index;
}
public void run() {
System.out.println("begin-" + index);
Thread.yield();
System.out.println("end-" + index);
}
}
public static void main(String[] args) {
YieldRunnable run1 = new YieldRunnable(1);
YieldRunnable run2 = new YieldRunnable(2);
run1.run();
run2.run();
}
}
Output
begin-1
end-1
begin-2
end-2Andrei
Level 41
What's the point of this exercise? I moved the Thread.yield(); around the code and the output was the same. I don't get it.
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
28 April 2021, 15:12solution
Runnable is not a thread but meant to be used in threads. Your test code would have to be like this:
+4
Andrei
28 April 2021, 16:55
I see, thank you!
0