我有3个帖子:
牙买加 斐 夏威夷 所有线程在从睡眠中唤醒时自行输出。
我希望夏威夷能够比其他时间长0.2秒,并且随着代码的继续,它会在另一个之后睡眠。示例:睡眠0.2秒长输出,再次睡眠0.4秒长时间输出,再次睡眠0.6秒,依此类推。
enter code here
public class Tw {
Tw( int t){
}
public static void main (String args[]) {
new S
("Jamaica").start();
new S("Fiji").start();
new S("Hawaii").start();
}
}
class S extends Thread {
String whatever;
public S(String str) {
super(str);
}
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(i + " " + getName());
try {
sleep((int)(Math.random() * 1000));;
} catch (InterruptedException e) {}
}
System.out.println("DONE! " + getName());
}
}
输出: 0牙买加 0夏威夷 0斐济 1牙买加 1夏威夷 2牙买加 1斐济 2斐济 3牙买加 4牙买加 3斐济 2夏威夷 5牙买加 4斐济 3夏威夷 4夏威夷 5夏威夷 6牙买加 7牙买加 5斐济 6夏威夷 6斐济 8牙买加 7斐济 7夏威夷 8斐济 9牙买加 8夏威夷 9斐济 9夏威夷 DONE!牙买加 DONE!斐 DONE!夏威夷