pipedcharacters.java

来自「Java Thread Programming (Source」· Java 代码 · 共 81 行

JAVA
81
字号
import java.io.*;

public class PipedCharacters extends Object {
	public static void writeStuff(Writer rawOut) {
		try {
			BufferedWriter out = new BufferedWriter(rawOut);
	
			String[][] line = {
					{ "Java", "has", "nice", "features." },
					{ "Pipes", "are", "interesting." },
					{ "Threads", "are", "fun", "in", "Java." },
					{ "Don't", "you", "think", "so?" }
				};

			for ( int i = 0; i < line.length; i++ ) {
				String[] word = line[i];

				for ( int j = 0; j < word.length; j++ ) {
					if ( j > 0 ) {
						// put a space between words
						out.write(" ");
					} 

					out.write(word[j]);
				}

				// mark the end of a line
				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();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?