?? sendcard1.java
字號:
//使用管道流在兩個線程對象之間傳數據。
import java.io.*;
public class SendCard1
{
public SendCard1() throws IOException
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
new Sender(out,12).start();
new Receiver(in).start();
}
public static void main (String args[]) throws IOException
{
new SendCard1();
}
}
class Sender extends Thread //發送線程
{
private PipedOutputStream out;
private int max;
public Sender(PipedOutputStream out,int max)
{
this.out= out;
this.max= max;
}
public Sender(PipedOutputStream out)
{
this(out,10);
}
public void run()
{
System.out.print("Sender: ");
int i=1;
try
{
while (i<=this.max)
{
this.out.write(i);
System.out.print(i+" ");
i++;
}
this.out.close(); //關閉管道輸出流
System.out.println(" out closed!");
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class Receiver extends Thread //接收線程
{
private PipedInputStream in;
public Receiver(PipedInputStream in)
{
this.in = in;
}
public void run()
{
System.out.print("Receiver: "+this.getName()+" ");
try
{
int i=-1;
do //輸入流未結束時
{
i = this.in.read();
System.out.print(i+" ");
}while (i!=-1);
this.in.close(); //關閉管道輸入流
System.out.println(" in closed!");
}
catch(IOException e)
{
System.out.println(e);
}
}
}
/*
析構方法??不自動執行
SendCard
public void finalize()// throws IOException //析構方法
{
System.out.println("finalize");
// try
// {
// in.close();
// out.close();
// }
// catch(IOException ioe)
//{ }
}
Sender
public void finalize() //析構方法??不自動執行
{
System.out.println("Sender finalize");
count--;
}
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -