views:

120

answers:

4

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.12.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.

+9  A: 

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.

Jon Skeet
I understood the problem. I like to use the "threadname + i" to create the new thread. i.e RunnableThread threadname1,2,..n = new RunnableThread(Name,Ip); Could you please guide me. So here for creating five threads instead of writing five time this statement I am trying to do it using a single line. Could you please help me.
Ricks
there is no way I know of to create a variable name based on a string, however I agree with Jon Skeet using a List is the best approach
Albinoswordfish
@Ricks - Your comment implies you do not understand the problem. You are attempting to declare TWO variables with the same name; "runname". Change one of the variable's name and your code will work. I also agree with Jon, though, that you should familiarize yourself more with Java and gain some experience before attempting to use threads. The problem you encountered was a compile-time error and is easily resolved. You said "it throws some error" which indicates you do not understand the Java language.
Gweebz
"runname" _could_ be a mishearing of a variable named "runnable".
Thorbjørn Ravn Andersen
+1  A: 

Probably a typo, you wanted to type :

String Name = "threadname" + i;
RunnableThread runname = new RunnableThread(Name,Ip);
fastcodejava
I think the OP wanted the variable names to be "threadname1", "threadname2",...
Albinoswordfish
+1  A: 

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 :

Colin Hebert
I wanted the variable runname to be runname1,runname2..runnamen. Not the passing parameters to the RunnableThread method. Please help.
Ricks
+1 for linking to Java Naming Conventions
Gweebz
@Ricks: As we've said, you can't do that. The name of the variable will be exactly what you write in the source code - it's not evaluated at execution time. You should be using a collection.
Jon Skeet
Thanks Colin HEBERT. This is what I am looking for the final edited code by you. I am all set now. Thanks
Ricks
I mean the final edited code by you is the real requirement for me. May be I did not convery that correctly. Sorry for the trouble to all of you. Thanks again.
Ricks
A: 

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.

Gweebz
Thanks for your help. This is just a sample code for which i am working on. I will be reading a input.txt file and it has around 1000 names and its ip address. I need to read those 1000 names,ip and create a thread to do some other task. So I am tyring to do this in a single line of thread creation. Could you please suggest me how can i do this.
Ricks