I have 3 very small classes.
The main class:
import java.io.*;
public class ConnectionManager {
public static void main(String argv[]) {
try {
PipedOutputStream pout = new PipedOutputStream();
PipedInputStream pin = new PipedInputStream(pout);
Sender s = new Sender(pout, true);
Receiver r = new Receiver(pin, true);
System.out.println("Starting threads");
s.start();
r.start();
} catch (Exception e) {}
}
}
The Sender class
import java.io.*;
import java.util.Random;
public class Sender extends Thread {
ObjectOutputStream oos;
boolean primitive;
public Sender(OutputStream os, boolean primitive) {
try {
oos = new ObjectOutputStream(os);
} catch (Exception e) {}
this.primitive = primitive;
}
public void run() {
Random rand = new Random();
while (true) {
try {
System.out.println("Integer is being sent");
oos.writeInt(10);
Thread.sleep(1000);
} catch (Exception e) {}
}
}
}
And the Receiver class
import java.io.*;
public class Receiver extends Thread {
ObjectInputStream ois;
boolean primitive;
public Receiver(InputStream is, boolean primitive) {
try {
ois = new ObjectInputStream(is);
} catch (Exception e) {}
this.primitive = primitive;
}
public void run() {
System.out.println("Receiver is starting");
while (true) {
try {
int x = ois.readInt();
System.out.print("An int was read: " + x);
} catch (Exception e) {}
}
}
}
Please ignore seemingly unused variables like primitive
and rand
. They're holdovers from slightly different versions that I was testing out earlier and I was too lazy to remove them.
Anyway, when I run the main method in ConnectionManager
, I get this as output:
Starting threads
Receiver is starting
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
Integer is being sent
//... ad infinitum
Why is the receiver thread not getting the messages that are piped through? What am I missing here?