2
配置文件分發數字範圍的各線程之間
ThreadSize = 10
StartRange = 1
EndRange = 1000
我有一個配置文件以上,其中我有線程數我想使用與客戶端實例是能夠使用ID範圍從1至1000並且假設客戶端線程被設置爲10,所以每個線程的範圍可以是100個id(基本上通過將結束範圍除以線程大小),它可以使用而不用跨過其他線程。所以我想是每個線程應使用100點的ID從範圍,而不對其他threads-步進例如
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() {
}
}
傳遞的開始和結束號碼爲每個線程在構造函數或添加一個方法給線程接受範圍。 –