Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的第二个线程没有运行?_Java_Multithreading_Synchronization_Countdownlatch - Fatal编程技术网

Java 为什么我的第二个线程没有运行?

Java 为什么我的第二个线程没有运行?,java,multithreading,synchronization,countdownlatch,Java,Multithreading,Synchronization,Countdownlatch,我有下面的程序,我想有3个线程的吸烟者等待代理。我正在尝试使用倒计时闩锁来实现这一点 public void runThreads(){ int numofTests; Scanner in = new Scanner(System.in); System.out.print("Enter the number of iterations to be completed:"); numofTests

我有下面的程序,我想有3个线程的吸烟者等待代理。我正在尝试使用倒计时闩锁来实现这一点

public void runThreads(){
            int numofTests;
            Scanner in = new Scanner(System.in);
            System.out.print("Enter the number of iterations to be completed:");
            numofTests = Integer.parseInt(in.nextLine());///Gets the number of tests from the user
            Agent agent = new Agent();
            Smoker Pam = new Smoker ("paper", "Pam");
            Smoker Tom = new Smoker ("tobacco", "Tom");
            Smoker Matt = new Smoker ("matches", "Matt");

            for(int i = 0; i < numofTests; i++){  //passes out as many rounds as the user specifies
                Pam.run();
                Tom.run();
                Matt.run();
                agent.run();
            }

您应该创建一个
线程
,并将
可运行
实例作为参数传递。另外,您应该调用
start
函数,而不是
run
。将代码更改为

(new Thread(Pam)).start();
//similar for others...

信息:

因为您没有任何线程。->你不知道为什么我认为我不需要显式定义线程,因为我认为通过实现runnable并调用run方法,它会隐式地创建/运行线程。谢谢你的邀请
(new Thread(Pam)).start();
//similar for others...