Perché il codice seguente non chiude dopo 1 secondo i cicli for che dal task manager vedo proseguire indisturbati?
codice:// An example that uses a Callable. import java.util.concurrent.*; class Esempio { public static void main(String args[]) { ExecutorService es = Executors.newFixedThreadPool(3); Future<Integer> f; Future<Double> f2; Future<Integer> f3; System.out.println("Starting"); f = es.submit(new Sum(100000000)); f2 = es.submit(new Hypot(3, 4)); f3 = es.submit(new Factorial(10)); try { System.out.println(f.get(1000, TimeUnit.MILLISECONDS)); System.out.println(f2.get(1000, TimeUnit.MILLISECONDS)); System.out.println(f3.get(1000, TimeUnit.MILLISECONDS)); } catch (InterruptedException exc) { System.out.println(exc); } catch (ExecutionException exc) { System.out.println(exc); } catch (TimeoutException exc) { System.out.println(exc); } es.shutdown(); System.out.println("Done"); } } // Following are three computational threads. class Sum implements Callable<Integer> { int stop; Sum(int v) { stop = v; } public Integer call() { int sum = 0; long g = 0; for (long y = 1; y <= 9_000_000_000_000_000_000L; y++) { if (y > 10) { g++; } else { g--; } } for (int i = 1; i <= stop; i++) { sum += i; } return sum; } } class Hypot implements Callable<Double> { double side1, side2; Hypot(double s1, double s2) { side1 = s1; side2 = s2; } public Double call() { long g = 0; for (long y = 1; y <= 9_000_000_000_000_000_000L; y++) { if (y > 10) { g++; } else { g--; } } return Math.sqrt((side1 * side1) + (side2 * side2)); } } class Factorial implements Callable<Integer> { int stop; Factorial(int v) { stop = v; } public Integer call() { int fact = 1; for (int i = 2; i <= stop; i++) { fact *= i; long g = 0; for (long y = 1; y <= 900_000_000L; y++) { if (y > 10) { g++; } else { g--; } } } return fact; } }
![]()

Rispondi quotando
