Halo an alle!
Ich habe ein Problem mit einer Aufgabe.
Ich muss die folgende Java code benutzen um dieses Ziel zu erreichen:
comandante_1 stellt eine Frage (immer als erstes), und soldatino_1 muss ihm antworten un dann auf Comandante_1 warten (Wait()).
Das Kode funktioniert bis ich :
ich ändere den Wert des Thread.sleep(500) (z.b. von 500 auf 5000)
und/oder
ich ändere die Reihenfolge in Main()
danach antworted das Programm nicht mehr (ich denke dass beide Threads sind im Wait(), auch wenn ich notifiyAll() nutze).
Ich bin neu in Java und brauche dringend Hilfe!
Danke an alle für euhre Hilfe!!
Strider
Ich habe ein Problem mit einer Aufgabe.
Ich muss die folgende Java code benutzen um dieses Ziel zu erreichen:
comandante_1 stellt eine Frage (immer als erstes), und soldatino_1 muss ihm antworten un dann auf Comandante_1 warten (Wait()).
Das Kode funktioniert bis ich :
ich ändere den Wert des Thread.sleep(500) (z.b. von 500 auf 5000)
und/oder
ich ändere die Reihenfolge in Main()
danach antworted das Programm nicht mehr (ich denke dass beide Threads sind im Wait(), auch wenn ich notifiyAll() nutze).
Ich bin neu in Java und brauche dringend Hilfe!
Danke an alle für euhre Hilfe!!
Strider
Java:
////////////////////////////////////////////////////
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();
}
}