有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

多线程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) 个答案

  1. # 1 楼答案

    <>初学者,而不是有一个除主方法之外没有任何别的类,并且实现可运行的,并且有男性和女性扩展A,考虑男性和女性只是执行可运行的自己。第二,您的男方法和女方法在其run方法中不包含任何内容,因此没有任何内容可以在另一个线程上运行。因此,你的线程一开始什么都不做