是否可以在一个循环中创建 n 个变量。有点像
for(int x = 0; int x < noOfThreads){
//create new threads here
}
我需要创建指定数量的线程作为运行时参数。不太确定如何去做。
谢谢
最佳答案
您可以在循环中创建并启动线程。
如果您需要保留对Thread
对象的引用,则将它们放在一个集合中,例如ArrayList
:
List<Thread> threads = new ArrayList<Thread>();
for(int x = 0; x < noOfThreads; x++){
Thread t = new Thread(/* ... */);
threads.add(t);
// do something with t
}
关于java - 创建n个变量java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8264065/