英文:
Nesting loops (Threads) in JAVA
问题
根据您提供的信息,您希望修改代码以使线程的输出与您期望的结果匹配。以下是您的代码的修改版本,以获得所需的输出:
public class Main {
private static String[] passwords = {"zero", "one", "two", "three", "four"};
public static void main(String[] args) {
int threadCount = 5; // 设置线程数量
Thread[] threads = new Thread[threadCount];
for (int i = 0; i < threadCount; i++) {
ClaimSnipe NS = new ClaimSnipe();
NS.password = passwords[i];
NS.i = i;
NS.DropTime = System.currentTimeMillis() + 1000;
Thread t = new Thread(NS);
threads[i] = t;
threads[i].setName(String.valueOf(i + 1));
NS.ThreadName = String.valueOf(threads[i].getName());
threads[i].start();
}
}
}
class ClaimSnipe implements Runnable {
private volatile boolean exit = false;
public String ThreadName;
public String password;
public int i;
public long DropTime;
public void stop() {
exit = true;
}
@Override
public void run() {
long currentTime = System.currentTimeMillis();
while (currentTime < (DropTime)) {
currentTime = System.currentTimeMillis();
}
System.out.println("Thread = " + ThreadName + "; i = " + i);
stop();
}
}
这个修改后的代码将创建5个线程,每个线程都使用一个不同的密码。现在,线程的输出应该与您所期望的匹配。
英文:
As my title states, i need to nest for loops (i am not sure if nesting is the right word i am looking for, but i need one for loop, inside the other)
The current code i have is
public class Main {
private static String[] passwords = {"zero", "one", "two", "three", "four"};
public static void main(String[] args) {
int threadds = 10;
Thread[] threads = new Thread[threadds];
for (int j = 0; j < passwords.length; j++) {
for (int i = 0; i < passwords.length; i++) {
ClaimSnipe NS = new ClaimSnipe();
NS.password = String.valueOf(i);
NS.i = i;
NS.DropTome = System.currentTimeMillis() + 1000;
Thread t = new Thread(NS);
threads[i] = t;
threads[i].setName(String.valueOf(i + 1));
NS.ThreadName = String.valueOf(threads[i].getName());
threads[i].start();
}
}
}
}
class ClaimSnipe implements Runnable {
private volatile boolean exit = false;
public String ThreadName;
public String password;
public int i;
public long DropTome;
public void stop() {
exit = true;
}
@Override
public void run() {
long unixTimern = System.currentTimeMillis();
while (unixTimern < (DropTome) - 1000) {
unixTimern = System.currentTimeMillis();
}
System.out.println("Thread = " + ThreadName + "; i = " + i);
stop();
}
}
The result it gives is
Thread = 0; i = 0
Thread = 0; i = 1
Thread = 0; i = 2
Thread = 0; i = 3
Thread = 0; i = 4
Thread = 1; i = 0
Thread = 1; i = 1
Thread = 1; i = 2
Thread = 1; i = 3
Thread = 1; i = 4
Thread = 2; i = 0
Thread = 2; i = 1
Thread = 2; i = 2
Thread = 2; i = 3
Thread = 2; i = 4
Thread = 3; i = 0
Thread = 3; i = 1
Thread = 3; i = 2
Thread = 3; i = 3
Thread = 3; i = 4
Thread = 4; i = 0
Thread = 4; i = 1
Thread = 4; i = 2
Thread = 4; i = 3
Thread = 4; i = 4
Process finished with exit code 0
^^^ What i want
-----------------------------------------------------
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 3; i = 2
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 3; i = 2
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 3; i = 2
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 3; i = 2
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 3; i = 2
Process finished with exit code 1
^^^ What i get...
Thread = 0; i = 0
Thread = 0; i = 1
Thread = 0; i = 2
Thread = 0; i = 3
Thread = 0; i = 4
Thread = 1; i = 0
Thread = 1; i = 1
Thread = 1; i = 2
Thread = 1; i = 3
Thread = 1; i = 4
Thread = 2; i = 0
Thread = 2; i = 1
Thread = 2; i = 2
Thread = 2; i = 3
Thread = 2; i = 4
Thread = 3; i = 0
Thread = 3; i = 1
Thread = 3; i = 2
Thread = 3; i = 3
Thread = 3; i = 4
Thread = 4; i = 0
Thread = 4; i = 1
Thread = 4; i = 2
Thread = 4; i = 3
Thread = 4; i = 4
Process finished with exit code 0
^^^ What i want
-----------------------------------------------------
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 3; i = 2
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 3; i = 2
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 3; i = 2
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 3; i = 2
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 1; i = 0
Thread = 2; i = 1
Thread = 4; i = 3
Thread = 5; i = 4
Thread = 3; i = 2
Process finished with exit code 1
^^^ What i get...
I have tried changing where i call the ClaimSnipe NS but all that did was make it so only one thread was used
答案1
得分: 1
"Your line 14 threads[i].setName(String.valueOf(i + 1));
- firstly adds 1 to your for loop counter variable and refers to the loop counter variable of your INNER FOR loop.
should be:
threads[i].setName(String.valueOf(j));
- which refers to the loop counter variable of your OUTER FOR loop and also does not increase it by 1.
This change will align your existing code to get your desired output. I hope this does the trick for you."
英文:
Your line 14 threads[i].setName(String.valueOf(i + 1));
- firstly adds 1 to your for loop counter variable and refers to the loop counter variable of your INNER FOR loop.
should be:
threads[i].setName(String.valueOf(j));
- which refers to loop counter variable of your OUTER FOR loop and also does not increase it by 1.
This change will align your existing code to get your desired output. I hope this does the trick for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论