Hallo Programmierer,
ich habe ein kleines Programm geschrieben, dass ein Rennen simuliert. Im Programm sind 2 Gruppen mit je 2 Rennern; Deutschland gegen Japan. Gedacht ist, dass der erste Renner 200 Metern rennen soll und der zweite die übrigen 200 Metern. Folgendes sind die 2 Klassen:
Klasse RaceDemo:
Zweite Klasse RaceRunnable
Irgendwann bekomme ich leider ein Exception
Kann mir jemand sagen woran es liegen kann, da ich leider nicht darauf kommen kann.
Danke im voraus
ich habe ein kleines Programm geschrieben, dass ein Rennen simuliert. Im Programm sind 2 Gruppen mit je 2 Rennern; Deutschland gegen Japan. Gedacht ist, dass der erste Renner 200 Metern rennen soll und der zweite die übrigen 200 Metern. Folgendes sind die 2 Klassen:
Klasse RaceDemo:
Code:
import java.lang.Thread.State;
import java.util.Map;
public class RaceDemo {
public static RaceRunnable race = new RaceRunnable();
public static ThreadGroup germany = new ThreadGroup("Germany");
public static Thread gRunner_1 = new Thread(germany, race, "First german runner");
public static Thread gRunner_2 = new Thread(germany, race, "Seconde german runner");
public static ThreadGroup japan = new ThreadGroup("Japan");
public static Thread jRunner_1 = new Thread(japan, race, "First japanese runner");
public static Thread jRunner_2 = new Thread(japan, race, "Seconde japanese runner");
public static void main(String[] args)
{
State gRunner_1State; State jRunner_1State;
State gRunner_2State; State jRunner_2State;
long gRaceTime = 0, jRaceTime = 0;
boolean gAlreadyIn = false, jAlreadyIn = false;
Map<Long, String> map = new HashMap<>();
gRunner_1.start();
jRunner_1.start();
while(true)
{
gRunner_1State = gRunner_1.getState();
gRunner_2State = gRunner_2.getState();
jRunner_1State = jRunner_1.getState();
jRunner_2State = jRunner_2.getState();
if (gRunner_2State.equals(State.TERMINATED) && !gAlreadyIn) {
gRaceTime = System.nanoTime();
System.out.println(gRaceTime + " mRunner_1-Status: " + gRunner_1State);
System.out.println(gRaceTime + " mRunner_2-Status: " + gRunner_2State);
gAlreadyIn = true;
}
if(jRunner_2State.equals(State.TERMINATED) && !jAlreadyIn)
{
jRaceTime = System.nanoTime();
System.out.println(jRaceTime + " jRunner_1-Status: " + jRunner_1State);
System.out.println(jRaceTime + " jRunner_2-Status: " + jRunner_2State);
jAlreadyIn = true;
}
if(gRunner_1State.equals(State.TERMINATED) && jRunner_1State.equals(State.TERMINATED))
{
break;
}
}
System.out.println("Endtime of the Group " + germany.getName() + " is: " + gRaceTime);
System.out.println("Endtime of the Group " + japan.getName() + " is: " + jRaceTime);
}
}
Zweite Klasse RaceRunnable
Code:
public class RaceRunnable implements Runnable {
public static String winner;
public static String firstRunnerReached200M;
public void race()
{
for (int distance = 1; distance <= 400; distance++) {
System.out.println("Distance covered by " + Thread.currentThread().getName() + " is: " + distance + " meters");
boolean isRaceWon = this.isRaceWon(distance);
if (isRaceWon) {
break;
}
boolean isHalfDistanceReached = this.isHalfDistanceReached(distance);
if (isHalfDistanceReached) {
try {
if(firstRunnerReached200M.equals(RaceDemo.gRunner_1.getName()))
{
RaceDemo.gRunner_2.start();
RaceDemo.gRunner_2.join();
}
else if(firstRunnerReached200M.equals(RaceDemo.jRunner_1.getName()))
{
RaceDemo.jRunner_2.start();
RaceDemo.jRunner_2.join();
}
} catch (InterruptedException ex) {
}
}
}
}
private boolean isRaceWon(int totalDistanceCovered)
{
boolean isRaceWon = false;
if(RaceRunnable.winner == null && totalDistanceCovered == 400)
{
String winnerName = Thread.currentThread().getName();
RaceRunnable.winner = winnerName;
System.out.println("Winner is: " + RaceRunnable.winner);
isRaceWon = true;
}
else if(RaceRunnable.winner == null)
{
isRaceWon = false;
}
else if(RaceRunnable.winner != null)
{
isRaceWon = true;
}
return isRaceWon;
}
private boolean isHalfDistanceReached(int halfDistance)
{
boolean isHalfDistanceReached = false;
if(RaceRunnable.firstRunnerReached200M == null && halfDistance == 200)
{
RaceRunnable.firstRunnerReached200M = Thread.currentThread().getName();
System.out.println("200 Meters reached by: " + RaceRunnable.firstRunnerReached200M);
isHalfDistanceReached = true;
}
else if(RaceRunnable.firstRunnerReached200M == null)
{
isHalfDistanceReached = false;
}
else if(RaceRunnable.firstRunnerReached200M != null)
{
isHalfDistanceReached = true;
}
return isHalfDistanceReached;
}
@Override
public void run()
{
this.race();
}
}
Irgendwann bekomme ich leider ein Exception
Code:
run:
Distance covered by First german runner is: 1 meters
Distance covered by First german runner is: 2 meters
Distance covered by First german runner is: 3 meters
Distance covered by First german runner is: 4 meters
Distance covered by First german runner is: 5 meters
Distance covered by First japanese runner is: 1 meters
Distance covered by First german runner is: 6 meters
Distance covered by First japanese runner is: 2 meters
Distance covered by First german runner is: 7 meters
Distance covered by First german runner is: 8 meters
Distance covered by First german runner is: 9 meters
Distance covered by First german runner is: 10 meters
Distance covered by First german runner is: 11 meters
Distance covered by First german runner is: 12 meters
Distance covered by First german runner is: 13 meters
Distance covered by First german runner is: 14 meters
Distance covered by First german runner is: 15 meters
Distance covered by First german runner is: 16 meters
Distance covered by First german runner is: 17 meters
Distance covered by First german runner is: 18 meters
Distance covered by First german runner is: 19 meters
Distance covered by First german runner is: 20 meters
Distance covered by First german runner is: 21 meters
Distance covered by First german runner is: 22 meters
Distance covered by First german runner is: 23 meters
Distance covered by First german runner is: 24 meters
Distance covered by First german runner is: 25 meters
Distance covered by First german runner is: 26 meters
Distance covered by First german runner is: 27 meters
Distance covered by First german runner is: 28 meters
Distance covered by First german runner is: 29 meters
Distance covered by First german runner is: 30 meters
Distance covered by First german runner is: 31 meters
Distance covered by First german runner is: 32 meters
Distance covered by First german runner is: 33 meters
Distance covered by First german runner is: 34 meters
Distance covered by First german runner is: 35 meters
Distance covered by First german runner is: 36 meters
Distance covered by First german runner is: 37 meters
Distance covered by First german runner is: 38 meters
Distance covered by First german runner is: 39 meters
Distance covered by First german runner is: 40 meters
Distance covered by First german runner is: 41 meters
Distance covered by First german runner is: 42 meters
Distance covered by First german runner is: 43 meters
Distance covered by First german runner is: 44 meters
Distance covered by First german runner is: 45 meters
Distance covered by First german runner is: 46 meters
Distance covered by First german runner is: 47 meters
Distance covered by First german runner is: 48 meters
Distance covered by First german runner is: 49 meters
Distance covered by First german runner is: 50 meters
Distance covered by First german runner is: 51 meters
Distance covered by First german runner is: 52 meters
Distance covered by First german runner is: 53 meters
Distance covered by First japanese runner is: 3 meters
Distance covered by First japanese runner is: 4 meters
Distance covered by First japanese runner is: 5 meters
Distance covered by First japanese runner is: 6 meters
Distance covered by First japanese runner is: 7 meters
Distance covered by First japanese runner is: 8 meters
Distance covered by First german runner is: 54 meters
Distance covered by First german runner is: 55 meters
Distance covered by First german runner is: 56 meters
Distance covered by First german runner is: 57 meters
Distance covered by First german runner is: 58 meters
Distance covered by First german runner is: 59 meters
Distance covered by First german runner is: 60 meters
Distance covered by First german runner is: 61 meters
Distance covered by First german runner is: 62 meters
Distance covered by First german runner is: 63 meters
Distance covered by First german runner is: 64 meters
Distance covered by First german runner is: 65 meters
Distance covered by First german runner is: 66 meters
Distance covered by First german runner is: 67 meters
Distance covered by First german runner is: 68 meters
Distance covered by First german runner is: 69 meters
Distance covered by First german runner is: 70 meters
Distance covered by First german runner is: 71 meters
Distance covered by First german runner is: 72 meters
Distance covered by First german runner is: 73 meters
Distance covered by First german runner is: 74 meters
Distance covered by First german runner is: 75 meters
Distance covered by First german runner is: 76 meters
Distance covered by First german runner is: 77 meters
Distance covered by First german runner is: 78 meters
Distance covered by First german runner is: 79 meters
Distance covered by First german runner is: 80 meters
Distance covered by First german runner is: 81 meters
Distance covered by First german runner is: 82 meters
Distance covered by First german runner is: 83 meters
Distance covered by First german runner is: 84 meters
Distance covered by First german runner is: 85 meters
Distance covered by First german runner is: 86 meters
Distance covered by First german runner is: 87 meters
Distance covered by First german runner is: 88 meters
Distance covered by First japanese runner is: 9 meters
Distance covered by First german runner is: 89 meters
Distance covered by First japanese runner is: 10 meters
Distance covered by First german runner is: 90 meters
Distance covered by First japanese runner is: 11 meters
Distance covered by First german runner is: 91 meters
Distance covered by First japanese runner is: 12 meters
Distance covered by First german runner is: 92 meters
Distance covered by First japanese runner is: 13 meters
Distance covered by First german runner is: 93 meters
Distance covered by First japanese runner is: 14 meters
Distance covered by First japanese runner is: 15 meters
Distance covered by First german runner is: 94 meters
Distance covered by First japanese runner is: 16 meters
Distance covered by First german runner is: 95 meters
Distance covered by First japanese runner is: 17 meters
Distance covered by First german runner is: 96 meters
Distance covered by First japanese runner is: 18 meters
Distance covered by First japanese runner is: 19 meters
Distance covered by First japanese runner is: 20 meters
Distance covered by First japanese runner is: 21 meters
Distance covered by First japanese runner is: 22 meters
Distance covered by First japanese runner is: 23 meters
Distance covered by First japanese runner is: 24 meters
Distance covered by First japanese runner is: 25 meters
Distance covered by First japanese runner is: 26 meters
Distance covered by First japanese runner is: 27 meters
Distance covered by First japanese runner is: 28 meters
Distance covered by First japanese runner is: 29 meters
Distance covered by First japanese runner is: 30 meters
Distance covered by First japanese runner is: 31 meters
Distance covered by First japanese runner is: 32 meters
Distance covered by First japanese runner is: 33 meters
Distance covered by First japanese runner is: 34 meters
Distance covered by First japanese runner is: 35 meters
Distance covered by First japanese runner is: 36 meters
Distance covered by First japanese runner is: 37 meters
Distance covered by First japanese runner is: 38 meters
Distance covered by First japanese runner is: 39 meters
Distance covered by First japanese runner is: 40 meters
Distance covered by First japanese runner is: 41 meters
Distance covered by First japanese runner is: 42 meters
Distance covered by First japanese runner is: 43 meters
Distance covered by First japanese runner is: 44 meters
Distance covered by First japanese runner is: 45 meters
Distance covered by First japanese runner is: 46 meters
Distance covered by First japanese runner is: 47 meters
Distance covered by First japanese runner is: 48 meters
Distance covered by First japanese runner is: 49 meters
Distance covered by First japanese runner is: 50 meters
Distance covered by First japanese runner is: 51 meters
Distance covered by First japanese runner is: 52 meters
Distance covered by First japanese runner is: 53 meters
Distance covered by First japanese runner is: 54 meters
Distance covered by First japanese runner is: 55 meters
Distance covered by First japanese runner is: 56 meters
Distance covered by First japanese runner is: 57 meters
Distance covered by First japanese runner is: 58 meters
Distance covered by First japanese runner is: 59 meters
Distance covered by First japanese runner is: 60 meters
Distance covered by First japanese runner is: 61 meters
Distance covered by First japanese runner is: 62 meters
Distance covered by First japanese runner is: 63 meters
Distance covered by First japanese runner is: 64 meters
Distance covered by First japanese runner is: 65 meters
Distance covered by First japanese runner is: 66 meters
Distance covered by First japanese runner is: 67 meters
Distance covered by First japanese runner is: 68 meters
Distance covered by First japanese runner is: 69 meters
Distance covered by First japanese runner is: 70 meters
Distance covered by First japanese runner is: 71 meters
Distance covered by First japanese runner is: 72 meters
Distance covered by First japanese runner is: 73 meters
Distance covered by First japanese runner is: 74 meters
Distance covered by First japanese runner is: 75 meters
Distance covered by First japanese runner is: 76 meters
Distance covered by First japanese runner is: 77 meters
Distance covered by First japanese runner is: 78 meters
Distance covered by First japanese runner is: 79 meters
Distance covered by First japanese runner is: 80 meters
Distance covered by First japanese runner is: 81 meters
Distance covered by First japanese runner is: 82 meters
Distance covered by First japanese runner is: 83 meters
Distance covered by First japanese runner is: 84 meters
Distance covered by First japanese runner is: 85 meters
Distance covered by First japanese runner is: 86 meters
Distance covered by First japanese runner is: 87 meters
Distance covered by First japanese runner is: 88 meters
Distance covered by First japanese runner is: 89 meters
Distance covered by First japanese runner is: 90 meters
Distance covered by First japanese runner is: 91 meters
Distance covered by First japanese runner is: 92 meters
Distance covered by First japanese runner is: 93 meters
Distance covered by First japanese runner is: 94 meters
Distance covered by First japanese runner is: 95 meters
Distance covered by First japanese runner is: 96 meters
Distance covered by First japanese runner is: 97 meters
Distance covered by First japanese runner is: 98 meters
Distance covered by First japanese runner is: 99 meters
Distance covered by First japanese runner is: 100 meters
Distance covered by First japanese runner is: 101 meters
Distance covered by First japanese runner is: 102 meters
Distance covered by First japanese runner is: 103 meters
Distance covered by First japanese runner is: 104 meters
Distance covered by First japanese runner is: 105 meters
Distance covered by First japanese runner is: 106 meters
Distance covered by First japanese runner is: 107 meters
Distance covered by First japanese runner is: 108 meters
Distance covered by First japanese runner is: 109 meters
Distance covered by First japanese runner is: 110 meters
Distance covered by First japanese runner is: 111 meters
Distance covered by First japanese runner is: 112 meters
Distance covered by First japanese runner is: 113 meters
Distance covered by First japanese runner is: 114 meters
Distance covered by First japanese runner is: 115 meters
Distance covered by First japanese runner is: 116 meters
Distance covered by First japanese runner is: 117 meters
Distance covered by First japanese runner is: 118 meters
Distance covered by First japanese runner is: 119 meters
Distance covered by First japanese runner is: 120 meters
Distance covered by First japanese runner is: 121 meters
Distance covered by First japanese runner is: 122 meters
Distance covered by First japanese runner is: 123 meters
Distance covered by First japanese runner is: 124 meters
Distance covered by First japanese runner is: 125 meters
Distance covered by First japanese runner is: 126 meters
Distance covered by First japanese runner is: 127 meters
Distance covered by First japanese runner is: 128 meters
Distance covered by First japanese runner is: 129 meters
Distance covered by First japanese runner is: 130 meters
Distance covered by First japanese runner is: 131 meters
Distance covered by First japanese runner is: 132 meters
Distance covered by First japanese runner is: 133 meters
Distance covered by First japanese runner is: 134 meters
Distance covered by First japanese runner is: 135 meters
Distance covered by First japanese runner is: 136 meters
Distance covered by First japanese runner is: 137 meters
Distance covered by First japanese runner is: 138 meters
Distance covered by First japanese runner is: 139 meters
Distance covered by First japanese runner is: 140 meters
Distance covered by First japanese runner is: 141 meters
Distance covered by First japanese runner is: 142 meters
Distance covered by First japanese runner is: 143 meters
Distance covered by First japanese runner is: 144 meters
Exception in thread "Seconde japanese runner" Exception in thread "First german runner" java.lang.IllegalThreadStateException
Distance covered by First japanese runner is: 145 meters
Distance covered by First japanese runner is: 146 meters
Distance covered by First japanese runner is: 147 meters
Distance covered by First japanese runner is: 148 meters
Distance covered by First japanese runner is: 149 meters
Distance covered by First japanese runner is: 150 meters
Distance covered by First japanese runner is: 151 meters
Distance covered by First japanese runner is: 152 meters
Distance covered by First japanese runner is: 153 meters
Distance covered by First japanese runner is: 154 meters
Distance covered by First japanese runner is: 155 meters
Distance covered by First japanese runner is: 156 meters
Distance covered by First japanese runner is: 157 meters
at java.lang.Thread.start(Thread.java:708Distance covered by First japanese runner is: 158 meters
)
Distance covered by First japanese runner is: 159 meters
at RaceRunnable.race(Distance covered by First japanese runner is: 160 meters
RaceRunnable.java:44)
Distance covered by First japanese runner is: 161 meters
at RaceRunnable.run(RaceRunnable.java:97)
Distance covered by First japanese runner is: 162 meters
Distance covered by First japanese runner is: 163 meters
at java.lang.Thread.run(Thread.java:748)
Distance covered by First japanese runner is: 164 meters
Distance covered by First japanese runner is: 165 meters
Exception in thread "First japanese runner" java.lang.IllegalThreadStateException
Distance covered by First japanese runner is: 166 meters
Distance covered by First japanese runner is: 167 meters
at java.lang.Thread.start(Thread.java:708)
Distance covered by First japanese runner is: 168 meters
at RaceRunnable.race(RaceRunnable.java:44Distance covered by First japanese runner is: 169 meters
)
Distance covered by First japanese runner is: 170 meters
at RaceRunnable.run(RaceRunnable.java:97)
Distance covered by First japanese runner is: 171 meters
Distance covered by First japanese runner is: 172 meters
Distance covered by First japanese runner is: 173 meters
at java.lang.Thread.run(Thread.java:748)
Distance covered by First japanese runner is: 174 meters
Distance covered by First japanese runner is: 175 meters
java.lang.IllegalThreadStateException
Distance covered by First japanese runner is: 176 meters
Distance covered by First japanese runner is: 177 meters
at java.lang.Thread.start(Thread.java:708)
Distance covered by First japanese runner is: 178 meters
at RaceRunnable.race(RaceRunnable.java:44)
Distance covered by First japanese runner is: 179 meters
at RaceRunnable.run(Distance covered by First japanese runner is: 180 meters
Distance covered by First japanese runner is: 181 meters
RaceRunnable.java:97)
Distance covered by First japanese runner is: 182 meters
Distance covered by First japanese runner is: 183 meters
at java.lang.Thread.run(Distance covered by First japanese runner is: 184 meters
Distance covered by First japanese runner is: 185 meters
Thread.java:748)
Distance covered by First japanese runner is: 186 meters
Distance covered by First japanese runner is: 187 meters
Distance covered by First japanese runner is: 188 meters
Distance covered by First japanese runner is: 189 meters
Distance covered by First japanese runner is: 190 meters
Distance covered by First japanese runner is: 191 meters
Distance covered by First japanese runner is: 192 meters
Distance covered by First japanese runner is: 193 meters
Distance covered by First japanese runner is: 194 meters
Distance covered by First japanese runner is: 195 meters
Distance covered by First japanese runner is: 196 meters
Distance covered by First japanese runner is: 197 meters
Distance covered by First japanese runner is: 198 meters
Distance covered by First japanese runner is: 199 meters
Distance covered by First japanese runner is: 200 meters
200 Meters reached by: First japanese runner
Distance covered by Seconde japanese runner is: 1 meters
Distance covered by First german runner is: 97 meters
Distance covered by First japanese runner is: 201 meters
832454651016399 jRunner_1-Status: WAITING
832454651016399 jRunner_2-Status: TERMINATED
Endtime of the Group Germany is: 0
Endtime of the Group Japan is: 832454651016399
BUILD SUCCESSFUL (total time: 0 seconds)
Kann mir jemand sagen woran es liegen kann, da ich leider nicht darauf kommen kann.
Danke im voraus