Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we speed-up CPU intensive tasks in java?

Task like finding factorial of 2000 where using BigInteger is a CPU-intensive task, is there anyway to speed-up such processes?

Ex: finding 2000! Since it is a single task only, i think there is no need of thread here( as running this program or running this task in a thread both has to perform such CPU-intensive things).

I've heard that Java 7 introduced a new parallel mechanism for compute intensive tasks. So, how do i perform this kind of things in it?

like image 571
cypronmaya Avatar asked Jan 06 '12 18:01

cypronmaya


People also ask

How can you maximize CPU utilization using Java programming?

You can modify the CPU load incurred by a Java program by inserting sleep statements (e.g. Thread. sleep() ) in your code, using variable delays to change the load. The simplest case would be a sleep statement in a loop, executed in a separate thread for each CPU core that you want to load.

How can I reduce CPU usage in Java?

You can set the priority of your whole Java application process to be lower than other applications; that's what the "nice" or "renice" commands do for your on Unix (or Mac OS X). Alternatively, and I think nicer, you can start a reduced-priority thread within your application, to do the CPU-intensive work.

Is Java CPU intensive?

java, that repeats a work unit of finding all prime numbers in a given integer range. It is a good example of CPU intensive process. Many Java applications are using the multithreading technology to run multiple processes of the same type concurrently instead of running them sequentially.

What causes high CPU in Java?

High CPU in java applications are due to huge volume of incoming requests or some intensive task performed within the code.


1 Answers

A factorial could be easily split to two tasks with a final merge. This is some kind of a map-reduce, if you like.

Example:

9! = (7*5*3*1) * (8*6*4*2)

So you can have two tasks.

This can be generalized into any amount of parallel tasks.

This solution has nothing to do with Java in specific, it's about converting "regular" solutions to parallel ones.

like image 113
Ron Klein Avatar answered Sep 30 '22 04:09

Ron Klein