⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 38.html

📁 写给JSP初级程序员的书
💻 HTML
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<meta name="description" content="Java,JDBC,EJB,Open Source,jdk,rmi">
<meta name="Keywords"
content="Java, servlets, Java servlet, Javascript, ActiveX, VRML,
        applet, applets, directory, news, jdbc, applications, 
        Java applications, Java developer, Java development, developer, 
        classes, Jars.com, Jars, intranet, Java applet, Javabeans, 
        Java products, JDK, Java development kit, java development environment, JIT,
        JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, 
        SUN, CGI, Perl, database, network, html,
        xml, dhtml, rating, ratings, review, jars, cgi, programming,
        software review, software rating">
<title>csdn_Java 中利用管道实现线程间的通讯</title>
<style>
.news {   BACKGROUND: #007cd3;  font-family: "宋体"; font-size: 9pt }
.t {  font-family: "宋体"; font-size: 9pt }
.t1 { color:#007cd3;  font-family: "宋体"; font-size: 9pt }
.white { font-family: "宋体"; font-size: 9pt;color:#FFFFFF }
.red { font-family: "宋体"; font-size: 9pt;color:#FF0000 }
A:visited {color:#0000FF}
A:hover {color: #ff6666; text-decoration: none}
.text {font-size: 12px; line-height: 160%; font-family: "宋体"}
.text1 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text1:visited {color:#000000}
.text1:hover {color: #000000}
.text2 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text2:visited {color:#000000}
.text2:hover {color: #000000}
.text3 {font-size: 12px; line-height: 100%; font-family: "宋体"; text-decoration: none}
.large {font-size: 14.8px; line-height: 130%}
</style>
</head>

<body


<center>




  <tr>
    <td WIDTH="100%" VALIGN="TOP">
      <tr>
        <td WIDTH="100%" CLASS="white"></td>
      </tr>
    
      <tr>
        <td WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" align="center" nowrap>Java 中利用管道实现线程间的通讯  </td>
   <p>     <td WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" align="center" nowrap>作者:jianada 来源 emook</p>

   </td>
      </tr>
      <tr>        <td WIDTH="100%" bordercolor="#FFFFFF" CLASS="t" bgcolor="#F0F0F0" colspan="2">
        <P><FONT color=#386490><SPAN class=line>  在java 
      语言中,提供了各种各样的输入输出流(stream),使我们能够很方便的对数据进行操作,其中,管道(pipe)流是一种特殊的流,用于在不同线程(threads)间直接传送数据。一个线程发送数据到输出管道,另一个线程从输入管道中读数据。通过使用管道,实现不同线程间的通讯。无需求助于类似临时文件之类的东西。本文在简要介绍管道的基本概念后,将以一个具体的实例pipeapp加以详细说明。<BR><BR><B>1. 
      管道的创建与使用</B><BR>  java提供了两个特殊的专门的类专门用于处理管道,它们就是pipedinputstream类和pipeoutputstream类。<BR>Pipedinputstream代表了数据在管道中的输出端,也就是线程向管道读数据的一端;pipeoutputstream代表了数据在管道中的输入端,也就是线程向管道写数据的一端,这两个类一起使用可以提供数据的管道流。<BR>  为了创建一个管道流,我们必须首先创建一个pipeoutstream对象,然后,创建pipeinputstream对象,实例如下:<BR>  pipeout= 
      new pipedyoutstream();<BR>  pipein= new 
      pipedputsteam(pipepout);<BR>  一旦创建了一个管道后,就可以象操作文件一样对管道进行数据的读写。<BR><BR><B>2. 
      演示程序: 
      pipeapp</B><BR>  应用程序由三个程序组成:主线程(pipeapp.java)及由主线程启动的两个二级线程(ythread.java和zthread.java),它们使用管道来处理数据。程序从一个内容为一行一行"x"字母的"input.txt"文件中读取数据,使用管道传输数据,第一次是利用线程ythread将数据"x"转换为"y",最后利用线程zthread将"y"转换为"z",之后,程序在屏幕上显示修改后的数据。<BR>  主线程 
      (pipeapp.java)<BR>  在main()方法中,程序首先创建一个应用对象:pipeapp pipeapp=new 
      pipeapp();<BR>由于程序中流操作都需要使用IOException异常处理,所以设置了一个try块。在try中,为了从源文件中读取数据,程序为"input.txt"文件创建了一个输入流Xfileln,:<BR>  fileinputstream 
      xfileln= new 
      fileinputstream("input.txt");<BR>  新的输入流传递给changetoy()方法,让线程ythread能读取该文件:<BR>  inputstream 
      ylnpipe 
      =pipeapp.changetoy(xfileln);<BR>  changetoy()方法创建将输入数据"x"改变到"y"的线程ythread,并返回该线程的输入管道:<BR>  inputstream 
      zlnpipe = 
      pipeapp.changetoz(ylnpipe);<BR>  changetoz()方法启动将数据从"y"改变到"z"的线程zehread,主程序将使用从changetoz()返回的输入管道。得到以修改的数据。<BR>  然后,程序将管道输入流定位到datainputstream对象,使程序能够使用readline()方法读取数据:<BR>  datainputstream 
      inputstream = new 
      datainputstream(zlnpiepe);<BR>  创建了输入流以后,程序就可以以行一行的读取数据病显示在屏幕上。<BR>  String 
      str= 
      inputstream.readline();<BR>  While(str!=null)<BR>  {<BR>    system.out.println(str);<BR>    str=inputstream.readline();<BR>  }<BR>  显示完成之后,程序关闭输入流:<BR>  inputstream.close();<BR>  changetoy()方法<BR>  changetoy()方法首先通过传递一个参数inputstream给datainputstream对象来定位资源的输入流,使程序能使用readline()方法从流中读取数据:<BR>  datainputstream 
      xfileln =new 
      datainutstream(inputstream);<BR>  然后,changetoy()创建输出管道和输入管道:<BR>  pipeoutstream 
      pipeout = new pipeoutputstream();<BR>  pipeinputstream pipeln = new 
      pipedinputsteam(pipeout);<BR>  为了能够使用println()方法输出修改的后的文本行到管道,程序将输出管道定位到printstream对象:<BR>  printstream 
      printstream = new 
      printstream(pipeout);<BR>  现在,程序可以创建将数据从x改变到y的线程,该线程是ythread类的一个对象,他传递两个参数:输入文件(xfileln)和输出管道(调用printstream)<BR>  ythread 
      ythread =new 
      thread(xfileln,printstream);<BR>  之后,程序启动线程:<BR>  changetoz()方法<BR>  changetoz()方法与changetoy()方法很相似,他从changetoy()返回的输入流开始:<BR>  datainputstream 
      yfileln= new 
      datainputstream(inputstream);<BR>  程序创建一个新的管道:<BR>  pipedoutstream 
      pipeout2 = new pipedoutputstream();<BR>  pipedinputstream pipeln2 = new 
      pipedinputsream(pipeout2);<BR>  该线程通过这个新的管道发出修改后的数据(输入流pipeln2)给主程序。 
      </SPAN></FONT></P>
      <P><SPAN class=line><FONT 
      color=#386490>源程序如下:<BR>//<BR>//pipeapp.java-pipeapp的主应用程序<BR>//<BR>import 
      java.io.*<BR>class pipeapp<BR>{<BR>public static void main(string[] 
      args)<BR>{<BR>pipeapp pipeapp=new 
      pipeapp();<BR>try<BR>{<BR>fileinputstream xfile =new 
      fileinputstream("input.txt");<BR>inputstream ylnpipe = 
      pipeapp.changetoy(xfileln);<BR>inputstream 
      zlnpipe=pipeapp.changetoz(ylnpipe);<BR>system.out.println();<BR>system.out.println("here 
      are the results");<BR>system.out.pringln();<BR>datainputstream inputstream 
      = nes datainputstream(zlnpipe);<BR>string str = 
      inputstream.readline();<BR>while 
      (str!=null)<BR>{<BR>system.out.println(str);<BR>str=inputstream.readline();<BR>}<BR>inputstream.close();<BR>}<BR>catch(exception 
      e)<BR>{<BR>system.out.println(e.tostring());<BR>}<BR>}<BR>public 
      inputstream changetoy(inputstream 
      inputstream)<BR>{<BR>try<BR>{<BR>datainputstream pipeout = new 
      datainputsteam(inputstream);<BR>pipedoutstream pipeout = new 
      pipedoutputstream();<BR>pipedlnsteam pipeln = new 
      pipedlnputstream(pipeout);<BR>printstream printstream = new 
      printstream(pipeout);<BR>ythread ythread = new 
      ythread(xfileln,printstream);<BR>ythread.start();<BR>return 
      pipeln;<BR>}<BR>catch(exeption 
      e)<BR>{<BR>system.out.println(x.tostring());<BR>}<BR>return 
      null;<BR>}<BR>public inputstream changetoz(inputstream 
      inputsteam)<BR>{<BR>try<BR>{<BR>datainputstream yfileln = new 
      datainputstream(inputstream);<BR>pipeoutputstream pipeln2 = new 
      pipedinputstream(pipeout2);<BR>printrstream printstream2 = new 
      printsteam(pipeout2);<BR>zthread zthread = new 
      zthread(yfileln,printstream2);<BR>zthread.start();<BR>return 
      pipeln2;<BR>}<BR>catch(exception 
      e)<BR>{<BR>system.out.println(e.tostring());<BR>}<BR>return 
      null;<BR>}<BR>}</FONT></SPAN></P>
      <P><SPAN class=line><FONT 
      color=#386490><B>Ythread类和Zthread类</B><BR>  由于ythread类与zthread类基本一样,在此仅以ythread为例加以说明。<BR>  Ythread的构造器接收两个参数:输入的文件和第一个管道的输出端,构造器存储这两个参数作为类的数据成员:<BR>  Ythread(datainputstream 
      xfileln,pringstream printstream)<BR>  {<BR>    this.xfileln = 
      xfileln;<BR>    this.printstream = 
      printstream;<BR>  }<BR>  线程通过run()方法来处理数据。首先读取一行数据,确保xstring不为空的情况下循环执行:<BR>string 
      xstring = xfileln.readline();<BR>  每读一行数据,完成一次转换<BR>  string ystring = 
      xstring.replace('x','y');<BR>  然后将修改后的数据输出到管道的输出端:<BR>  prinstream.prinrln(ystring);<BR>  为了确保所有缓冲区的数据完全进入管道的输出端:<BR>  pringstram.flush();<BR>  循环完成后,线程关闭管道输出流:<BR>  pringstram.close();<BR><BR>ythread类的源程序如下:<BR>//<BR>//ythread.java<BR>//<BR>import 
      java.io.*;<BR>class ythread exteads thread<BR>{<BR>datainputstream 
      xfileln;<BR>pringstream printstream;<BR>ythread(datainputstream 
      xfileln,pringstream.printstream)<BR>{<BR>this.xfileln = 
      xfileln;<BR>this.printstream = printstream;<BR>}<BR>public void 
      run()<BR>{<BR>try<BR>{<BR>string xstring = 
      xfileln.readline();<BR>while(xstring!=null)<BR>{<BR>string ystring= 
      xstring.replace('x','y');<BR>printstream.pringln(ystring);<BR>printstream.flush();<BR>xstring= 
      xfileln.readline();<BR>}<BR>printstream.close();<BR>}<BR>catch{ioexception 
      e}<BR>{<BR>system.out.println(e.tostring());<BR>}<BR>}<BR>}<BR>pipeapp应用程序使用microsoft 
      visual j++1.1编译</FONT></SPAN></P>
        </td>
      </tr>
    </td>
  </tr>
</div>
</body>
</html>

⌨️ 快捷键说明

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