Java 为什么我的第二个线程没有运行?
我有下面的程序,我想有3个线程的吸烟者等待代理。我正在尝试使用倒计时闩锁来实现这一点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
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...