maxb_pro
maxb_pro

Reputation: 53

java spring endless concurrent work

I'm working on app which has to send http request. App has to send 5 request for 100 000 users in 1 day. I use spring mvc and I think to use threads to do this http request

  for(int j = 0; j < 100; j++){

        for(int i = 0; i < 5000; i++){
            OrderActionThread thread = new OrderActionThread();
            thread.start();
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

OrderActionThread makes http request

I don't know what I should use for this task... I've read about Spring integration but I'm not sure I should use it. How can I decide this task?

Upvotes: 0

Views: 78

Answers (1)

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27516

Don't use Thread directly, your loop above will create 5 000 000 threads, you will most likely run out of memory, thread pools are best for such big number of threads.

And because you want to schedule it every day I would suggest to use Executors.newScheduledThreadPool:

final ScheduledExecutorService pool = Executors.newScheduledThreadPool(10);
for(int j = 0; j < 100; j++){
    for(int i = 0; i < 5000; i++){
        pool.scheduleAtFixedRate(new OrderActionThread(), 0, 1, TimeUnit.DAYS);
     }
 }

The above pool will use 10 threads (you can increase that number) and will schedule all 5 000 000 tasks to repeat every 24 hours (1 TimeUnit.DAYS).

Upvotes: 1

Related Questions