java - 创建n个变量java

标签 java multithreading concurrency

是否可以在一个循环中创建 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/

相关文章:

node.js - MongoDB 数据库信号量和 Node.js Process.NextTick()

java - 在 Java 中显示树层次结构及其值

Java查找树中寻找节点的路径

Java 扫描仪找不到下一行?

python - 在 PyQt 中使用自定义小部件?

java - 如何在 CountDownLatch.await() 中断时取消关联的 Runnables

java - 在 getter 中添加到 ArrayList 时出现 ConcurrentModificationException

java - 使用计时器倒计时暂停和恢复

ruby 线程 : how to join threads from different places so that they are concurrent?

multithreading - Nhibernate中的多线程UnitOfWork