août
2010
Java Quiz #41 : time attack !
(FR)
La cellule d'expertise de votre entreprise a développé un Timer pour que vous puissiez mesurer les performances de votre application, mais... ils ont juste oublié de fournir une méthode pour récupérer la valeur du compteur ! Comment faire pour l'afficher une fois la mesure prise ?
Vous ne pouvez intervenir que sur la classe Quiz41, et il est interdit de modifier le code existant ou d'utiliser la Réflexion.
Bonne chance !
(EN)
The Architects Team developed a Timer class to help you measure the performance of your code, but... they forgot to provide a method to get the timer's value ! How can you get it back and print it in the console ?
You can only alter the Quiz41 class ; modifying the already existing code or using Reflection is forbidden.
Good luck !
public class Quiz41 { public static void main(String[] args) throws InterruptedException { Timer timer = new Timer(); timer.start(); Thread.sleep(new Random().nextInt(2000)); timer.stop(); } }
public class Timer { private TimerThread thread = new TimerThread(); public void start() { thread.start(); } public void stop() { thread.stop(); } public static class TimerThread extends Thread { private AtomicInteger counter = new AtomicInteger(0); @Override public void run() { try { while (!isInterrupted()) { counter.incrementAndGet(); Thread.sleep(100); } } catch (InterruptedException e) { interrupt(); } } public int getCounter() { return counter.get(); } } }
