配置文件
ThreadSize = 10
StartRange = 1
EndRange = 1000
我在上面有一个配置文件,其中我有我想要使用的线程数,并且客户端实例能够使用从 1 到 1000 的 ID 范围,并假设客户端线程设置为 10,因此每个线程的 ID 范围为 100 (基本上通过将结束范围除以线程大小)它可以在不踩其他线程的情况下使用。所以我想要的是每个线程应该使用该范围内的 100 个 id 而不会踩到其他线程 - 例如
Thread1 will use 1 to 100 (id's)
// generate a random number between 1 to 100 and keep on printing values until it has generated all the random values between 1 to 100
Thread2 will use 101 to 200 (id's)
// generate a random number between 101 to 200 and keep on printing values until it has generated all the random values between 101 to 200
Thread3 will use 201 to 300 (id's)
// generate a random number between 201 to 300 and keep on printing values until it has generated all the random values between 201 to 300
-----
----
Thread10 will use 901 to 1000
// generate a random number between 901 to 1000 and keep on printing values until it has generated all the random values between 901 to 1000
我知道如何编写多线程程序,但不知道应该如何划分各个线程之间的范围。
public static void main(String[] args) {
for (int i = 1; i <= threadSize; i++) {
new Thread(new ThreadTask(i)).start();
}
}
class ThreadTask implements Runnable {
private int id;
public ThreadTask(int id) {
this.id = id;
}
public synchronized void run() {
}
}