简体   繁体   English

Java从for循环使用不同的名称运行多个线程

[英]Java run multiple threads from for loop with different names

I am trying to start a variable number of threads within a for loop and want to name the threads prime1, prime2, [...]. 我试图在for循环中启动可变数量的线程,并想将线程命名为prime1,prime2,[...]。

The PrimeFinderThread Class extends from Thread. PrimeFinderThread类是从Thread扩展的。

[...]

    for (int i = 0; i <= numberThreads; i++) {
         PrimeFinderThread ("prime" + i) = new PrimeFinderThread (lowerBoundary, interval); 
    }

[...]

I am getting the error: 我收到错误消息:

The left-hand side of an assignment must be a variable. 作业的左侧必须是变量。

from ("prime" + i) 来自("prime" + i)

What is a possible soloution to start X Threads with a seperate name for each? 用不同的名称启动X线程的可能解决方案是什么?

Try the following: 请尝试以下操作:

Thread[] threads = new PrimeFinderThread[numberThreads];
for (int i = 0; i < numberThreads; i++) {
    threads[i] = new PrimeFinderThread (lowerBoundary, interval);
    threads[i].setName("prime" + i);
    threads[i].start();
}

It sets the name via the setName -method and then starts each thread via a call to start . 它通过setName -method设置名称,然后通过调用startstart每个线程。

However, it is probably easier to make the constructor of PrimeFinderThread take an extra argument which is the name and invoke the super class constructor. 但是,使PrimeFinderThread的构造PrimeFinderThread接受一个额外的参数(即name并调用超类构造函数可能更容易。

public PrimeFinderThread (String name, int lowerBoundary, int interval) {
    super(name); // instead of setName
    // Do the other stuff
}

IMO, an even better approach is to not extend the Thread class but rather provide a Runnable , preferably with some kind of ExecutorService - check eg the Executors JavaDoc or the concurrency trail from the Oracle website. IMO,一种更好的方法是扩展Thread类,而是提供Runnable ,最好提供某种ExecutorService检查例如Executors JavaDoc或Oracle网站上的并发跟踪

For an assignment in Java you cannot specify additional parameters on the left hand side of the = operator. 对于Java中的赋值,您不能在=运算符的左侧指定其他参数。 So PrimeFinderThread ("prime" + i) is and will stay invalid. 因此PrimeFinderThread ("prime" + i)是并且将保持无效。

Instead you should simply define an additional parameter for your constructor, and pass that to the parent class constructor using the super keyword construct. 相反,您应该简单地为构造函数定义一个附加参数,然后使用super关键字结构将其传递给父类构造函数。

You should assign the created threads to some variable 您应该将创建的线程分配给某个变量

PrimeFinderThread[] threads = new PrimeFinderThread[numberThreads];
for (int i = 0; i < numberThreads; i++) {
    threads[i] = new PrimeFinderThread ("prime" + (i+1), lowerBoundary, interval); 
    threads[i].start();
}

As for setting the name of each thread, that depends on what constructors your PrimeFinderThread class has. 至于设置每个线程的名称,这取决于PrimeFinderThread类具有哪些构造函数。 You can pass the thread name to the constructor of PrimeFinderThread , and from there, pass it to the constructor of Thread . 您可以将线程名称传递给PrimeFinderThread的构造PrimeFinderThread ,然后从那里将其传递给Thread的构造函数。

For example: 例如:

public PrimeFinderThread (String name, int lowerBoundary, int interval)
{
    super (name);
    ...
}

You cannot specify additional parameters with X in left side assignment in Java. 在Java的左侧分配中,不能使用X指定其他参数。 To give name to your threads you can call super(threadName) from the sub class of thread or thread.setName("threadName") can be used. 要给线程命名,您可以从线程的子类或线程中调用super(threadName)。可以使用setName(“ threadName”)。

public class ThreadSubClass extends Thread{

    public ThreadSubClass(String threadName){
        super(threadName);
    }

    @Override
    public void run(){
        System.out.println("Entering : " + getName());
        //do Something
    }


    public static void main(String [] args){
        for(int i=0;i<5;i++){
            ThreadSubClass t = new ThreadSubClass("Prime"+i);
            t.start();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM