[英]Divide each thread to use ID in a particular range
我正在做一個項目,我需要確保每個線程在特定范圍內每次都獲得唯一的ID
。 例如-
如果number of threads is 2
number of tasks is 10
則每個線程將執行10個任務。 因此,這意味着2個線程將執行20個任務。
所以我正在尋找這樣的東西-
在上面的示例中,第一個線程應使用id between 1 and 10
,第二個線程應使用id between 11 and 20
。
另一個例子。 因此,假設我有3 thread
且number of tasks as 20
則第一個線程應使用ID在1 and 20
之間,第二個線程應在21 and 40
之間21 and 40
而第三個線程在41 and 60
之間。
下面是到目前為止我擁有的代碼,它的作用是將其作為AtomicInteger
獲得ID,並且每次從1開始我都會獲得唯一ID。
class ThreadTask implements Runnable {
private final AtomicInteger id;
private int noOfTasks;
public ThreadTask(AtomicInteger id2, int noOfTasks) {
this.id = id2;
this.noOfTasks = noOfTasks;
}
@Override
public void run() {
while(noOfTasks > 0) {
System.out.println(id.getAndIncrement());
// And use this id for other things.
noOfTasks--;
}
}
}
public class XMPTest {
public static void main(String[] args) {
final int noOfThreads = 2;
final int noOfTasks = 10;
final AtomicInteger id = new AtomicInteger(1);
//create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
// queue some tasks
for (int i = 0; i < noOfThreads; i++) {
service.submit(new ThreadTask(id, noOfTasks));
}
}
}
我如何限制它,以使線程1的ID在1 and 10
之間,第二個線程的ID在11 and 20
之間
更新的代碼:-我正在嘗試以下代碼
public static void main(String[] args) {
final int noOfThreads = 2;
final int noOfTasks = 10;
//create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
// queue some tasks
for (int i = 0, int nextId = 1; i < noOfThreads; i++, nextId += noOfTasks) {
service.submit(new ThreadTask(nextId, noOfTasks));
}
}
class ThreadTask implements Runnable {
private final int id;
private int noOfTasks;
public ThreadTask(int nextId, int noOfTasks) {
this.id = nextId;
this.noOfTasks = noOfTasks;
}
public void run() {
for (int i = id; i <= noOfTasks; i++) {
System.out.println(i);
}
}
}
因此,對於第一個線程,它正在打印出1-10,這很好。 但是在第二個線程中,nextId為11,noOfTasks為10,因此我的for循環將第二次在run方法中工作。
在啟動線程之前,請在單線程代碼中分配所需的范圍,然后將范圍傳遞給每個線程。
例如:
public static void main(String[] args) {
final int noOfThreads = 2;
final int noOfTasks = 10;
//create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
// queue some tasks
for (int i = 0, int nextId = 1; i < noOfThreads; i++, nextId += noOfTasks) {
service.submit(new ThreadTask(nextId, noOfTasks));
}
}
class ThreadTask implements Runnable {
private final int id;
private int noOfTasks;
public ThreadTask(int nextId, int noOfTasks) {
this.id = nextId;
this.noOfTasks = noOfTasks;
}
public void run() {
for (int i = id; i < id + noOfTasks; i++) {
System.out.println(i);
}
}
}
您將需要增加id,因此它不能是最終的。 我也看不到為什么需要使用AtomicInteger,因為Dispatcher線程中沒有爭用(在這種情況下,該線程處理主要功能)。 如果您仍然需要使用它,這就是我要做的。
public class XMPTest {
public static void main(String[] args) {
final int noOfThreads = 2;
final int noOfTasks = 10;
AtomicInteger id = new AtomicInteger(1);
//create thread pool with given size
ExecutorService service = Executors.newFixedThreadPool(noOfThreads);
// queue some tasks
for (int i = 0; i < noOfThreads; i++) {
service.submit(new ThreadTask(id, noOfTasks));
id.addAndGet(1);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.