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.