Ciao a tutti, ho un problema con un compitino che devo fare e non riesco a trovare il bandolo della matassa.
Mi é stato chiesto di utilizzare il seguente codice, per fare in pratica questo:
Commander1 chiede una domanda (sempre per primo), e il soldato gli risponde e quindi va in Wait fino alla prossima domanda.
Il programma sembra fgunzionare, ma se io cambio :
il valore di Thread.sleep(500)
oppure
l'ordine delle chiamate in Main
il programma non risponde piú (penso che entrambe le Thread vanno in Wait (nonostante il NotifyAll().).
Aiuto devo consegnare il compitino e non trovo il bandolo... Grazie a tutti in anticipo per l'aiuto!
Strider

codice:
//********************//

public class soldatino_1 implements Runnable 
{
     private final String[] ANSWERS = { "25", "2", "All of us are Sir!" };
     private int nextAnswer = 0;
     private comandante_1 mycomandante;
     
    public soldatino_1() 
    {
//        super();
    }
    
    /** Second Constructor */
    public soldatino_1(comandante_1 c_1) 
    {
//        super();
        mycomandante = c_1;
    }

     /** Sets the answer number */
    private synchronized void answerQuestion()
    {
        while(!mycomandante.isWaitingForAnswer())
        { 
            try
            {
                wait();
            }
            catch(InterruptedException e)
            {
                String errMessage = e.getMessage();
                System.out.println(errMessage);
            }
        }        
        nextAnswer = mycomandante.getNextQuestion();
        mycomandante.sir(); 
        System.out.println("soldatino (A" + nextAnswer + "):" + ANSWERS[nextAnswer]);    
        notifyAll();            
    }
    
    /** Override the Run method*/
    public void run()
    {
        for(int i = 0; i <3; i++)
        {
            try
            {
                Thread.sleep(500);
            }
            catch(InterruptedException e)
            {
                String errMessage = e.getMessage();
                System.out.println(errMessage);
            }                    
            answerQuestion();
        }
    }
}

//********************//

import java.util.Random;

public class comandante_1 extends Thread
{
    private final String[] Questions = { "How old are you?", "How many fitness tests have you taken?", "Are you prepared for the war?" };
    private int nextQuestion = 0;
    private Random generator = new Random();
    private boolean waitingForAnswer = false;
    
    /** Creates a new instance of comandante */
    public comandante_1() 
    {
//        super();
    }
    
    /** Returns an int value set randomly using the Random class */
    private int setNextQuestion()
    {
        int r = generator.nextInt(3);
        return r;
    }
    
    /** Sets the question number randomly using the setNextQuestion method */
    private synchronized void askQuestion()
    {           
        while(isWaitingForAnswer())
        {
            try
            {
                wait();
            }
            catch(InterruptedException e)
            {
                String errMessage = e.getMessage();
                System.out.println(errMessage);
            }
        }
        nextQuestion = setNextQuestion();
        setWaitingForAnswer(true);
        System.out.println("comandante (Q" + nextQuestion + "):" + Questions[nextQuestion]);
        notifyAll();              
    }
    
    /** Override the Run method*/
    public void run()
    {
        for(int i = 0; i <3; i++)
        {            
            try
            {
                Thread.sleep(500);
            }
            catch(InterruptedException e)
            {
                String errMessage = e.getMessage();
                System.out.println(errMessage);
            }                    
            askQuestion();
        }
    }
    
    // Private Setter method:
    private void setWaitingForAnswer(boolean b)
    {
        waitingForAnswer = b;
    }
    
    // Public method Sir:
    public void sir()
    {
        setWaitingForAnswer(false);
    }
    
    // Pubblic getter method:    
    public boolean isWaitingForAnswer()
    {
        return waitingForAnswer;
    }
        
    // Public method getNextQuestion:
    public int getNextQuestion()
    {
        return nextQuestion;
    }
}

//**************************//

public class Main
{
   public static void main(String[] args) 
   {    
       Comandante_1 com_1 = new Comandante_1();
       Soldatino_1 sol_1 = new Soldatino_1(com_1);
       Thread thre_1 = new Thread(sol_1);   
       
       com_1.start();
       thre_1.start();

   }   
}