Pregunta

Estoy usando siguiente código para obtener la suma de comprobación de un archivo. Se utiliza 50% de la CPU durante los cálculos.

MessageDigest md = MessageDigest.getInstance("MD5");
InputStream is = new FileInputStream("C:\\Temp\\Small\\Movie.mp4"); // Size 700 MB

byte [] buffer = new byte [blockSize];
int numRead;
do 
{
 numRead = is.read(buffer);
 if (numRead > 0) 
 {
  md.update(buffer, 0, numRead);
 }
} while (numRead != -1);

byte[] digest = md.digest();

¿Qué se puede hacer para reducir el código de uso de CPU máximo que no sea Thread.sleep (ms)

cordiales, Kingsley Reuben J

¿Fue útil?

Solución

You could use the Thread.setPriority(int newPriority) method to reduce the thread's priority. This will result in other higher-priority threads being executed more often. However, your MD5 will not be calculated as quickly as if you left the priority alone -- why wouldn't you want this calculation to complete as quickly as possible?

EDIT: Here is a "Fast MD5" implementation, which boasts a significant performance increase (26% faster on average) over Java's default java.security.MessageDigest implementation. See the author's page for detailed information, including code examples and benchmarks. The code is available under the GNU LGPL 2.1 license.

Otros consejos

I'd rather dedicate priority management to os, for windows you could start your app with

start /low your_executable
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top