Java/Threads/File IO Threads

Материал из Java эксперт
Перейти к: навигация, поиск

BufferedWriter and threads

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.Reader;
import java.io.Writer;
public class PipedCharacters extends Object {
  public static void writeStuff(Writer rawOut) {
    try {
      BufferedWriter out = new BufferedWriter(rawOut);
      String[][] line = { { "Java", "Source", "and", "Support." }};
      for (int i = 0; i < line.length; i++) {
        String[] word = line[i];
        for (int j = 0; j < word.length; j++) {
          out.write(word[j]);
        }
        out.newLine();
      }
      out.flush();
      out.close();
    } catch (IOException x) {
      x.printStackTrace();
    }
  }
  public static void readStuff(Reader rawIn) {
    try {
      BufferedReader in = new BufferedReader(rawIn);
      String line;
      while ((line = in.readLine()) != null) {
        System.out.println("read line: " + line);
      }
      System.out.println("Read all data from the pipe");
    } catch (IOException x) {
      x.printStackTrace();
    }
  }
  public static void main(String[] args) {
    try {
      final PipedWriter out = new PipedWriter();
      final PipedReader in = new PipedReader(out);
      Runnable runA = new Runnable() {
        public void run() {
          writeStuff(out);
        }
      };
      Thread threadA = new Thread(runA, "threadA");
      threadA.start();
      Runnable runB = new Runnable() {
        public void run() {
          readStuff(in);
        }
      };
      Thread threadB = new Thread(runB, "threadB");
      threadB.start();
    } catch (IOException x) {
      x.printStackTrace();
    }
  }
}





Output stream and threads

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedBytes extends Object {
  public static void writeStuff(OutputStream rawOut) {
    try {
      DataOutputStream out = new DataOutputStream(
          new BufferedOutputStream(rawOut));
      int[] data = { 82, 105, 99, 104, 97, 114, 100, 32, 72, 121, 100,
          101 };
      for (int i = 0; i < data.length; i++) {
        out.writeInt(data[i]);
      }
      out.flush();
      out.close();
    } catch (IOException x) {
      x.printStackTrace();
    }
  }
  public static void readStuff(InputStream rawIn) {
    try {
      DataInputStream in = new DataInputStream(new BufferedInputStream(
          rawIn));
      boolean eof = false;
      while (!eof) {
        try {
          int i = in.readInt();
          System.out.println("just read: " + i);
        } catch (EOFException eofx) {
          eof = true;
        }
      }
      System.out.println("Read all data from the pipe");
    } catch (IOException x) {
      x.printStackTrace();
    }
  }
  public static void main(String[] args) {
    try {
      final PipedOutputStream out = new PipedOutputStream();
      final PipedInputStream in = new PipedInputStream(out);
      Runnable runA = new Runnable() {
        public void run() {
          writeStuff(out);
        }
      };
      Thread threadA = new Thread(runA, "threadA");
      threadA.start();
      Runnable runB = new Runnable() {
        public void run() {
          readStuff(in);
        }
      };
      Thread threadB = new Thread(runB, "threadB");
      threadB.start();
    } catch (IOException x) {
      x.printStackTrace();
    }
  }
}





Using pipes for inter-thread I/O

// : c13:PipedIO.java
// Using pipes for inter-thread I/O
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
import java.util.Timer;
import java.util.TimerTask;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.util.Random;
class Sender extends Thread {
  private Random rand = new Random();
  private PipedWriter out = new PipedWriter();
  public PipedWriter getPipedWriter() {
    return out;
  }
  public void run() {
    while (true) {
      for (char c = "A"; c <= "z"; c++) {
        try {
          out.write(c);
          sleep(rand.nextInt(500));
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }
  }
}
class Receiver extends Thread {
  private PipedReader in;
  public Receiver(Sender sender) throws IOException {
    in = new PipedReader(sender.getPipedWriter());
  }
  public void run() {
    try {
      while (true) {
        // Blocks until characters are there:
        System.out.println("Read: " + (char) in.read());
      }
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}
public class PipedIO {
  public static void main(String[] args) throws Exception {
    Sender sender = new Sender();
    Receiver receiver = new Receiver(sender);
    sender.start();
    receiver.start();
    new Timeout(4000, "Terminated");
  }
} ///:~
class Timeout extends Timer {
  public Timeout(int delay, final String msg) {
    super(true); // Daemon thread
    schedule(new TimerTask() {
      public void run() {
        System.out.println(msg);
        System.exit(0);
      }
    }, delay);
  }
} ///:~