import java.util.Random;
public class goat_rodeo {
static public int the_count=10;
static public int the_number=0;
public static void main(String[] args) {
Producer_g p1 = new Producer_g();
Consumer_g c1 = new Consumer_g();
p1.start();
c1.start();
}
}
class Consumer_g extends Thread {
public Consumer_g() {
}
public void run() {
while (true) {
if (goat_rodeo.the_number == goat_rodeo.the_count) {
System.out.println("Consumer #" + goat_rodeo.the_number);
return;
} else {
System.out.println("Consumer #" + goat_rodeo.the_number);
try {
sleep(new Random().nextInt(100)+10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Producer_g extends Thread {
public Producer_g() {
}
public void run() {
while (true) {
if (goat_rodeo.the_number == goat_rodeo.the_count) return;
else {
goat_rodeo.the_number++;
System.out.println("Producer #" + goat_rodeo.the_number);
try {
sleep(new Random().nextInt(100)+10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
Two Runs