你好主要的想法是,如果男性>女性,女性線程必須等待1000毫秒(1秒),然後再次檢查。我試圖尋找這個,但我無法找到解決方案。可能有人請幫助我嗎?(我剛開始學習線程)Java線程sleep()的幫助
public class A implements Runnable {
public void run(){
}
public static void main(String[] args)throws InterruptedException {
int n = 3;
int m = 17;
int f = 13;
Thread th1 = new Thread(new male(m,n));
Thread th2 = new Thread(new female(f,n));
th1.start();
th2.start();
//not working
if (m>f){
th2.sleep(1000);
}
else if(f>m){
th1.sleep(1000);
}
}
}
class male extends A {
public male(int male, int count){
while(male>0){
male -= count;
System.out.println("m: " + male);
}
}
}
class female extends A {
public female(int female, int count){
while(female>0){
female -= count;
System.out.println("f: " + female);
}
}
}
插入明顯的性別歧視笑話,女性可以在這裏等待任何東西。 – 2014-12-02 22:25:18
如果您遵循以下準則,您的代碼將更具可讀性:http://www.oracle.com/technetwork/java/codeconventions-135099.html – 2014-12-02 22:25:34
我不知道爲什麼,但我看到了這個問題的'其他'圖像: O – 2014-12-02 22:30:16