帮帮我~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
————————————————————————————————————————————
睡了一觉起来之后,再回来看这个题目。豁然开朗,只需要把thread。sleep(500) 挪到 while(true)下面就行了。
有些时候当你感到无能为力的时候请不要放弃,换个心情再重新上路,加油朋友们。
当其他人都盲目追寻真理的时候,记住,万事皆虚。
当其他人的思想都被法律与道德所束缚的时候,记住,万事皆允。
————————————————————————————————
When everyone else blindly pursues the truth, remember that everything is false.
When other people's minds are bound by law and morality, remember that everything is acceptable.
————————————————————————————————
package zh.codegym.task.task16.task1626;
public class Solution {
public static int number = 5;
public static void main(String[] args) throws InterruptedException {
Thread a = new Thread(new CountUpRunnable(), "增加");
a.start();
a.join();
new Thread(new CountdownRunnable(), "减少").start();
}
public static class CountUpRunnable implements Runnable{
private int countdownIndex = 1;
@Override
public void run() {
try {
while (true) {
System.out.println(toString());
++countdownIndex ;
if (countdownIndex == 6) return;
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String toString() {
return Thread.currentThread().getName() + ": " + countdownIndex;
}
}
public static class CountdownRunnable implements Runnable {
private int countdownIndex = Solution.number;
public void run() {
try {
while (true) {
System.out.println(toString());
countdownIndex -= 1;
if (countdownIndex == 0) return;
Thread.sleep(500);
}
} catch (InterruptedException ignored) {
}
}
public String toString() {
return Thread.currentThread().getName() + ": " + countdownIndex;
}
}
}