Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 划分每个线程以在特定范围内使用ID_Java_Multithreading - Fatal编程技术网

Java 划分每个线程以在特定范围内使用ID

Java 划分每个线程以在特定范围内使用ID,java,multithreading,Java,Multithreading,我正在从事一个项目,在这个项目中,我需要确保每个线程在特定范围内每次都获得唯一的ID。比如说- 如果线程数为2,任务数为10,则每个线程将执行10个任务。这意味着2个线程将完成20个任务 所以我要找的是这样的东西- 在上面的示例中,第一个线程应该使用介于1和10之间的id,第二个线程应该使用介于11和20之间的id 另一个例子。因此,假设我有3个线程和20个任务,那么第一个线程应该在1和20之间使用ID,第二个线程应该在21和40之间,第三个线程在41和60之间 下面是到目前为止我所拥有的代码,

我正在从事一个项目,在这个项目中,我需要确保每个线程在特定范围内每次都获得唯一的
ID
。比如说-

如果
线程数为2,任务数为10,则每个线程将执行10个任务。这意味着2个线程将完成20个任务

所以我要找的是这样的东西-

在上面的示例中,第一个线程应该使用介于1和10之间的
id
,第二个线程应该使用介于11和20之间的
id

另一个例子。因此,假设我有
3个线程
20个任务,那么第一个线程应该在
1和20之间使用ID,第二个线程应该在
21和40之间,第三个线程在
41和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-10,这很好。但是在第二个线程中,nextId是11,noOfTasks是10,所以我的for循环第二次在run方法中工作。

您需要增加id,因此它不能是final。 我也不明白为什么需要使用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);
    }
}
公共类XMPTest{
公共静态void main(字符串[]args){
最终int noOfThreads=2;
最终int noOfTasks=10;
AtomicInteger id=新的AtomicInteger(1);
//创建具有给定大小的线程池
ExecutorService=Executors.newFixedThreadPool(noOfThreads);
//将一些任务排队
for(inti=0;i

}

在启动线程之前,根据需要在单线程代码中拆分范围,然后将范围传递给每个线程

例如:

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);
        }
    }
}
publicstaticvoidmain(字符串[]args){
最终int noOfThreads=2;
最终int noOfTasks=10;
//创建具有给定大小的线程池
ExecutorService=Executors.newFixedThreadPool(noOfThreads);
//将一些任务排队
for(inti=0,intnextid=1;i
“我需要确保每个线程在特定范围内每次都获得唯一的ID。”为什么?这里有一种“糟糕的代码味道”。我试图确保每个线程都独立于其他线程在不同的范围内工作。因为我们计划进行负载和性能测试,所以我们的前辈已经告诉我了。确保每个线程都分配了一个范围,并且它只在该范围内工作。我尝试了这个,但它也打印出了21个。它不应该打印出来,对吗?在您的示例中,它将确保第一个线程的范围为
1到10
,然后第二个线程的范围为
11到20
?我还发现了他的代码的一个问题。我已经用我的代码更新了问题。此代码有一个问题。第二次,它将是nextId为11,numberOftasks为10。那么我的for循环在run方法中是如何工作的呢?我已经用代码更新了我的问题。对,你需要使用id范围
[id,id+noOfTasks)
,而不是
[id,noOfTasks]
。我在上面的更新中提供了代码。
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);
        }
    }
}