使用不同的唯一ID的每个线程的性能改进 [英] Performance Improvement for each thread using different unique id

查看:89
本文介绍了使用不同的唯一ID的每个线程的性能改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述是:-

Problem Statement is:-

每个线程使用的唯一ID在1到1000之间,程序必须运行60分钟或更长时间,因此在这60分钟内,所有ID都有可能完成,因此我需要再次使用这些ID,

我知道几种方法,一种方法是我从StackOverflow的帮助下编写的,但是当我尝试运行此方法时,发现的是,经过几分钟的运行,该程序变得非常慢并且在控制台上打印ID需要花费大量时间.而且有时我还会收到OutOfMemory错误.有没有更好的方法来解决此类问题?

I know several ways to do it, one way is the below that I wrote by taking help from StackOverflow, but when I tried running this, what I found is that, after few minutes of run this program gets very slow and it takes lot of time to print the ID on the console. And also I get OutOfMemory Error sometimes. Is there any better way to solve this kind of problem?

class IdPool {
    private final LinkedList<Integer> availableExistingIds = new LinkedList<Integer>();

    public IdPool() {
        for (int i = 1; i <= 1000; i++) {
            availableExistingIds.add(i);
        }
    }

    public synchronized Integer getExistingId() {
        return availableExistingIds.removeFirst();
    }

    public synchronized void releaseExistingId(Integer id) {
        availableExistingIds.add(id);
    }
}


class ThreadNewTask implements Runnable {
    private IdPool idPool;

    public ThreadNewTask(IdPool idPool) {
        this.idPool = idPool;
    }

    public void run() {
        Integer id = idPool.getExistingId();
        someMethod(id);
        idPool.releaseExistingId(id);
    }

    private void someMethod(Integer id) {
        System.out.println("Task: " +id);
    }
}

public class TestingPool {
    public static void main(String[] args) throws InterruptedException {
        int size = 10;
        int durationOfRun = 60;
        IdPool idPool = new IdPool();   
        // create thread pool with given size
        // create thread pool with given size
    ExecutorService service = new ThreadPoolExecutor(size, size, 500L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), new ThreadPoolExecutor.CallerRunsPolicy()); 


        // queue some tasks
        long startTime = System.currentTimeMillis();
        long endTime = startTime + (durationOfRun * 60 * 1000L);

        // Running it for 60 minutes
        while(System.currentTimeMillis() <= endTime) {
            service.submit(new ThreadNewTask(idPool));
        }

        // wait for termination        
        service.shutdown();
        service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS); 
    }
}

推荐答案

在上一个问题中,我已经向您解释了您的代码向执行程序提交了数百万个任务,因为它在60分钟内循环提交了任务,但没有提现.等待.

I already explained you in your previous question that your code submitted millions and millions of tasks to the executor, since it submits tasks in a loop during 60 minutes, withot waiting.

目前还不清楚您的最终目标是什么,但实际上,您正在填充任务队列,直到没有可用的内存为止.由于您没有解释程序的目标,因此很难为您提供任何解决方案.

It's very unclear what your end goal is, but as is, you're filling a queue of tasks until you don't have any memory available anymore. Since you don't explain the goal of your program, it's hard to give you any solution.

但是您可以做的第一件事是限制执行程序的任务队列的大小.这将在每次队列满时强制主线程阻塞.

But the first thing you could do is to limit the size of the task queue of your executor. This would force the main thread to block each time the queue is full.

这篇关于使用不同的唯一ID的每个线程的性能改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆