Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    Problema chiusura thread

    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;
        }
    }


  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da giannino1995 Visualizza il messaggio
    Perché il codice seguente non chiude dopo 1 secondo i cicli for che dal task manager vedo proseguire indisturbati?
    "The shutdown() method will allow previously submitted tasks to execute before terminating"
    e
    "Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted."
    Ultima modifica di andbin; 12-12-2013 a 17:04
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Grazie per il messagio. Quindi per chiudere tutto dopo 1 secondo come devo fare?

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da giannino1995 Visualizza il messaggio
    Grazie per il messagio. Quindi per chiudere tutto dopo 1 secondo come devo fare?
    Intanto hai 3 get(1000, TimeUnit.MILLISECONDS), quindi 3 secondi (al massimo, all'incirca).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.