我正在进行一个项目,在该项目中,我需要确保每个线程每次都在特定范围内获得唯一的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
,那么第一个线程应该使用1 and 20
之间的ID,第二个线程应该在21 and 40
之间,第三个线程在41 and 60
之间。
下面是我到目前为止的代码,它所做的是它将获得id作为AtomicInteger
,并且我从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应该在1 and 10
之间获得id,而第二个线程应该在11 and 20
之间获得id
更新代码:-我正在尝试下面的代码
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,因此它不能是最终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);
}
}
}