多线程Java Thread sleep()帮助
您好,主要的想法是,如果男性>;内螺纹,内螺纹必须等待1000毫秒(1秒),然后再次检查。我试着寻找这个,但找不到解决办法。谁能帮帮我吗?(我刚刚开始学习线程)
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);
}
}
}
# 1 楼答案