- What is snk?
snk is short for sink. A sink is any consumer, eg: a PipedReader ‘consumes’ the character data placed on the pipe by the ‘source’: a PipedWriter.
I want to practice pipe myself. I want to write a simple program that has two threads, These thread read from two files, One thread transfers data to another thread by pipe.
import java.io.*;
class SimpleThread extends Thread {
public SimpleThread(String str) {
super(str);
}
public void run() {
FileInputStream in = null;
InputStreamReader inchar=null;
try {
in = new FileInputStream("myfile1.txt");
inchar = new InputStreamReader(in);
System.out.println("Text from file ");
int c;
while ((c = inchar.read()) != -1) {
System.out.print((char)c);
}
}
catch(Exception e) {
System.out.println("Runnable terminating with exception" + e );
} finally {
try{
inchar.close();
}
catch(Exception e) {
System.out.println("Runnable terminating with exception" + e );
}
}
}
}
class SimpleThread1 extends Thread {
public SimpleThread1(String str) {
super(str);
}
public void run() {
FileInputStream in = null;
InputStreamReader inchar=null;
try {
in = new FileInputStream("myfile2.txt");
inchar = new InputStreamReader(in);
System.out.println("Text from file ");
int c;
while ((c = inchar.read()) != -1) {
System.out.print((char)c);
}
}
catch(Exception e) {
System.out.println("Runnable terminating with exception" + e );
} finally {
try{
inchar.close();
}
catch(Exception e) {
System.out.println("Runnable terminating with exception" + e );
}
}
}
}
public class Main {
public static void main(String[] args) throws IOException{
new SimpleThread("Jamaica").start();
new SimpleThread1("Jamaica").start();
}
}
I don’t know how to use pipe in this code, Please guide me
start with creating your piped writer and reader.
You could do this in your main method.
PipedReader snk = new PipedReader();
PipedWriter writer = new PipedWriter(snk);
then construct SimpleThread with the writer.
and then contruct SimpleThread1 with the reader.
// Start the thread for the second level analysis. It
// will take data from buf_in and write it into pipe_out.
new Analyze1Thread (pipe_out, buf_in).start ();
But the pipeout is not a string nor doesn’t relate to runnable interface.
Well, that certainly makes my time spent writing an example for you worth it.
You seem to be unclear on the concept of extending a class.
class SimpleThread1 extends [B]PipedWriter[/B] implements Runnable {
private PipedReader in;
... code continues, as a Reader? You liar! You [i]just[/i] said it would be a writer!
A - Here’s a perfect example of why meaningful class, method and variable names are a really good idea. SimpleThread? Really? Just name it Fred next time and save us the heartache.
B - When we say that we’re extending a class, by using extends, our class now is the class we’re extending (note: constructors do not come across unless asked for explicitly with super())
C - Later, in your main, when you create your two threads, you connect the writer to the reader. Of course, you threw that away by enveloping an additional reader inside your writer and a writer in your reader. So the actual reader/writer pair is connected, but you don’t use them. Rather, you create another reader and another writer that know absolutely nothing about each other.
It’s a rather simple concept: I am the writer, you are the reader, and the internet is the pipe.
Read my code, copy my code, play with my code, see if you can add file reading and writing to my code, and for goodness sake - abandon your hopeless mess, it’s just not worth untangling it.
public class SimpleThread implements Runnable {
private PipedWriter out=new PipedWriter();
public PipedWriter getPipedWriter(){ return out;}
public void run() {
FileInputStream in = null;
InputStreamReader inchar=null;
BufferedReader a;
try {
in = new FileInputStream("myfile1.txt");
inchar = new InputStreamReader(in);
a=new BufferedReader(inchar);
int c;
while ((c = a.read()) != -1) {
out.write(c);
}
out.flush();
}
catch(Exception e) {
System.out.println("Runnable terminating with exception" + e );
}
}
}
class SimpleThread1 implements Runnable {
private PipedReader in;
int i;
public SimpleThread1(SimpleThread a) throws IOException{
in=new PipedReader(a.getPipedWriter());
}
public void run() {
try {
BufferedWriter outc=null;
OutputStreamWriter out=null;
outc=new BufferedWriter
(new OutputStreamWriter(new FileOutputStream("myfile2.txt"),"UTF8"));
int c;
while ((c = in.read()) != -1) {
outc.write((char)c);
}
out.flush();
}
catch(Exception e) {
System.out.println("Runnable terminating with exception " + e );
e.printStackTrace();
}
}
}
public class Main {
public static void main(String[] args) throws IOException{
SimpleThread a=new SimpleThread();
SimpleThread1 b=new SimpleThread1(a);
new Thread(a).start();
new Thread(b).start();
}
}
Error
Runnable terminating with exception java.io.IOException: Write end dead
java.io.IOException: Write end dead
at java.io.PipedReader.read(PipedReader.java:224)
at pipecheck.SimpleThread1.run(SimpleThread1.java:29)
at java.lang.Thread.run(Thread.java:619)
I read that pipe can be used in order to transfer data between JAVA and C or JAVA and perl. Please explain about mechanism. How java can get data from another process by pipe?