¿Es necesario este bloque sincronizado?

No es necesario porque está esperando la señal de "hecho". Esa memoria vacía de una manera que todos los valores del hilo esperado se vuelven visibles para el hilo principal.

Sin embargo, puede probarlo fácilmente, hacer dentro del run method a computation that takes several (millions) steps and don't get optimized by the compiler, if you see a value different than from the final value that you expect then your final value was not already visible to main thread. Of course here the critical part is to make sure the computation doesn't get optimized so a simple "increment" is likely to get optimized. This in general is usefull to test concurrency where you are not sure if you have correct memory barriers so it may turn usefull to you later.

Respuesta 1

synchronized is not needed around System.out.println(number);, but not because the PrintWriter.println() implementations are internally synchronized or because by the time doneSignal.await() unblocks all the worker threads have finished.

synchronized is not needed because there's a happens-before edge between everything before each call to doneSignal.countDown and the completion of doneSignal.await(). This guarantees that you'll successfully see the correct value of number.

Answer:2

I have been recently running into an issue in which my web application will not start properly and the stack trace doesn't indicate exactly what happened. I have been able to isolate it to an event ...

Estoy trabajando en un desafío de codificación en una aplicación bancaria donde necesito obtener el número de transacciones en los últimos 60 segundos. Para eso estoy usando java.sql.Timestamp como clave de un mapa como el siguiente: Mapa y ...

Estamos utilizando CVS para alrededor de 50 proyectos java que desarrollamos con Eclipse y construimos con Hudson. Ahora hemos llegado al punto en el que queremos migrar a algo mejor, y estoy considerando ...

Tengo las siguientes líneas de código Java: Object [] results = api.getProjects (); for (int i = 0; i <results.length; i ++) {System.out.println (results [i] .toString ()); } Y aquí está el ...