开发者

Why am I getting an "xxx is already defined" compiler error?

开发者 https://www.devze.com 2023-01-15 22:05 出处:网络
I am trying to create multiple threads using variable like threadname1, threadname2,..threadnamen. Instead of giving it as a hard coded value I am trying to do it using a for loop for the n numbers an

I am trying to create multiple threads using variable like threadname1, threadname2,..threadnamen. Instead of giving it as a hard coded value I am trying to do it using a for loop for the n numbers and use that at the end of "threadname" string. It throws some error. How do I fix this issue?

public class RunnableExample{

    public static void main(String[] args){
        String Name = "";
        String Ip = "";
        for (int i=1; i<=2; i++){
            if(i == 1){
                Name = "irony";
                Ip = "82.209.27.24";
            }
            else{
                Name = "jocky";
                Ip = "98.12.098.56";
            }
            String runname = "threadname" + i;
            RunnableThread runname = new RunnableThread(Name,Ip);
              new Thread(runname).start();
        }

        //RunnableThread threadname1 = new RunnableThread("irony","82.209.27.24");
        //RunnableThread thread4 = new RunnableThread("jocky","98.开发者_如何学Python12.098.56");
        //new Thread(threadname1).start();
        //new Thread(threadname2).start();
        try{

        }
        catch (InterruptedException e) {

        }
    }

Output:

bash-3.00# javac RunnableExample.java
RunnableExample.java:43: runname is already defined in main(java.lang.String[])
            RunnableThread runname = new RunnableThread(Name,Ip);

How do I resolve this issue? Maybe some typecasting is required it seems. I am not sure.


This is your problem:

String runname = "threadname" + i;
RunnableThread runname = new RunnableThread(Name,Ip);

You're trying to declare two variables with the same name. You can't do that. Change the name of one of the variables. The names of variables are fixed at compile-time in Java. You can't say "declare a variable with the name of the execution-time value of this variable" which is what I think you're trying to do.

If you want a way of accessing multiple values, use a collection or an array. For example, you might want a List<RunnableThread> here - add the value to the list in each iteration of your loop.

I'd also strongly recommend that you make sure you understand the basics of Java (things like variables and collections) before you start try experiment with threading. Threading is complicated and can be very hard to reason about - it's going to be even harder if you're struggling with the core language.


Probably a typo, you wanted to type :

String Name = "threadname" + i;
RunnableThread runname = new RunnableThread(Name,Ip);


You have to change the name of your variable containing the thread name :

        String threadName = "threadname" + i;
        RunnableThread runname = new RunnableThread(threadName, ip);

A good thing to do if you work with Java is to use the Java naming convention. For example all variable start with a lower case.


You might have wanted to do this :

import java.util.HashMap;
import java.util.Map;

public class RunnableExample {

    public static void main(String[] args) {

        Map<String, RunnableThread> threadMap = new HashMap<String, RunnableThread>();

        String name = "";
        String ip = "";
        for (int i = 1; i <= 2; i++) {
            if (i == 1) {
                name = "irony";
                ip = "82.209.27.24";
            } else {
                name = "jocky";
                ip = "98.12.098.56";
            }
            String threadName = "threadname" + i;
            RunnableThread thread = new RunnableThread(name, ip);
            new Thread(thread).start();

            threadMap.put(threadName, thread);
        }

        threadMap.get("threadname1").getIp();
    }
}

Resources :

  • Oracle.com - Naming Conventions


I won't bother pointing out the problem of your posted code because others already have. I will however suggest you do not use a for loop if you are going to be doing if checks inside it to see which iteration you are on. This is not a good practice. Infact, a non iterative solution is actually less lines of code and cleaner...

Thread ironyThread = new RunnableThread("irony", "82.209.27.24");
Thread jockyThread = new RunnableThread("jocky", "98.12.098.56");
ironyThread.start();
jockyThread.start();

Something like that will do what you are trying. I know you said that you want it to handle N threads. My solution would still be cleaner that the pattern you are following; adding an if check around each iteration for different threadName values.

0

精彩评论

暂无评论...
验证码 换一张
取 消