Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Errori con java thread

  1. #1
    Utente di HTML.it L'avatar di hopeway
    Registrato dal
    Sep 2017
    residenza
    Catania
    Messaggi
    47

    Errori con java thread

    Salve, sto avendo dei problemi nella creazione di un semplice programmino per la gestione di thread, non riesco a risolvere per via di un errore di NullPointerException in riga 61 e 80

    codice:
    public class prova {    public static void main(String[] args){
            Shared n = new Shared();
    
    
            Thread_I a = new Thread_I();
            Thread_D b = new Thread_D();
    
    
            Thread tI = new Thread(a,"tI");
            Thread tD = new Thread(b,"tD");
    
    
            tI.start();
            tD.start();
        }
    }
    
    
    
    
    
    
    class Shared {
        private int n = 100;
        
        public synchronized boolean somma(int incremento){
            String threadName = Thread.currentThread().getName();
            n+=incremento;
            System.out.println(threadName + "valore di n = "+n);
            
            if(n>150){
                System.out.println("Fine del thread! " + threadName);
                return false;
            }
            
            return true;
        }
    
    
        public synchronized boolean sottrai(int decremento){
            String threadName = Thread.currentThread().getName();
            n-=decremento;
            System.out.println(threadName + " valore di n = "+n);
    
    
            if(n<80){
                System.out.println("Fine del thread " + threadName);
                return false;
            }
            return true;
        }
    }
    
    
    class Thread_I implements Runnable {
        Shared n;
    
    
        public void Thread_I (Shared n) {this.n=n;}
    
    
        public void run() {
            int i = 0;
            while(i<1000){
                try{
                    Thread.sleep(100);
                }catch (InterruptedException e) {}
    			
                int inc = (int)(Math.random()*10);
    			
    			if (! n.somma(inc)) return;
                i++;
            }
        }
    }
    
    
    class Thread_D implements Runnable {
        Shared n;
    
    
        public void Thread_D (Shared n) {this.n=n;}
    
    
        public void run () {
            int i = 0;
            while(i<1000){
                try{
                    Thread.sleep(300);
                }catch (InterruptedException e) {}
                int decr = (int) (Math.random()*10);
    
    
                if(!n.sottrai(decr)) return;
                i++;
            }
        }
    }

  2. #2
    Utente di HTML.it L'avatar di hopeway
    Registrato dal
    Sep 2017
    residenza
    Catania
    Messaggi
    47
    ho risolto, ho erroneamente messo il void nel costruttore. Qualcuno potrebbe spiegarmi, ormai che ho aperto il thread, qualcosa di più riguardo il NullPointerException?

  3. #3
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,254
    Quote Originariamente inviata da hopeway Visualizza il messaggio
    codice:
        public static void main(String[] args){
            Shared n = new Shared();
    
    
            Thread_I a = new Thread_I();
            Thread_D b = new Thread_D();
    
    
            Thread tI = new Thread(a,"tI");
            Thread tD = new Thread(b,"tD");
    
    
            tI.start();
            tD.start();
        }
    Innanzitutto una questione: hai una Shared n .... dove stai usando nel main questa variabile n?
    Appunto ... non la stavi usando.

    E non la stavi usando perché avevi messo "void" dove c'è il costruttore di Thread_I/Thread_D, facendo diventare questi dei metodi come altri. E lasciando quindi il costruttore di "default" che non ha argomenti. Cosa che appunto ti permetteva di invocare

    new Thread_I()

    senza passare argomenti.

    Ma se togli il void, i due costruttori sono:

    public Thread_I (Shared n) {this.n=n;}
    public Thread_D (Shared n) {this.n=n;}

    Quindi DEVI passare a questi l'oggetto Shared.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java 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 © 2024 vBulletin Solutions, Inc. All rights reserved.