Nesting loops (Threads) in JAVA

huangapple go评论112阅读模式
英文:

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 = {&quot;zero&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;};
public static void main(String[] args) {
int threadds = 10;
Thread[] threads = new Thread[threadds];
for (int j = 0; j &lt; passwords.length; j++) {
for (int i = 0; i &lt; 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 &lt; (DropTome) - 1000) {
unixTimern = System.currentTimeMillis();
}
System.out.println(&quot;Thread = &quot; + ThreadName + &quot;; i = &quot; + 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.

huangapple
  • 本文由 发表于 2020年7月30日 20:34:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63173300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定