线程类Thread的API接口分析系列之管道通讯Piped

    xiaoxiao2021-03-25  158

    Java对于多线程之间的通信提供了一个机制,管道,虽然说线程之间完全可以通过共享变量进行通信,但是java提供了这么一个机制也是应该了解下的,毕竟专门提供的机制肯定有它的好处。由于相对简单(估计内部其实也是用了线程之间的共享变量之类的)就没必要分析什么了,直接上例子

    对于字节流:PipedInputStream和PipedOutputStream

    public class ThreadApiTest implementsRunnable {

     

             privateString type;

             privateObject obj;

            

             ThreadApiTest(Objectobj){

                       this.obj=obj;

             }

             /**

             *多线程相关api测试

             *@author : zhengrf1

              * @throws InterruptedException

             *@date 创建时间:2017年3月7日下午4:43:15

             */

             publicstatic void main(String[] args) throws InterruptedException {

                       //TODO Auto-generated method stub

                       PipedInputStreamin = new PipedInputStream();

                     PipedOutputStream out = newPipedOutputStream();

                       ThreadApiTestinThread = new ThreadApiTest(in);

                       ThreadApiTestoutThread = new ThreadApiTest(out);

                       inThread.type="read";

                       outThread.type="write";

                       Threadintest = new Thread(inThread);

                       Threadouttest = new Thread(outThread);

                       try{

                                in.connect(out);

                       }catch (IOException e) {

                                //TODO Auto-generated catch block

                                e.printStackTrace();

                       }

     

                       outtest.start();

                       Thread.sleep(1000);

                       intest.start();

             }

     

             /*@author : zhengrf1

              * @date 创建时间:2017年3月7日 下午4:46:48

              * @see java.lang.Runnable#run()

              */

             @Override

             publicvoid run() {

                       //TODO Auto-generated method stub

                       if(type.equals("write")){

                                write();

                       }

                       else{

                                read();

                       }

             }

            

             publicvoid write(){

                       PipedOutputStreamin = (PipedOutputStream)obj;

                       try{

                                out.write("hello".getBytes());

                       }catch (IOException e) {

                                //TODO Auto-generated catch block

                                e.printStackTrace();

                       }

             }

            

             publicvoid read(){

                       PipedInputStreamin = (PipedInputStream)obj;

                       try{

                                byte[]b = new byte[50];

                                System.out.println(in.read(b));

                                Strings = new String(b,"utf-8");

                                System.out.println(s);

                       }catch (IOException e) {

                                //TODO Auto-generated catch block

                                e.printStackTrace();

                       }

             }

    }

     

    对于字符流:PipedReader和PipedWriter

    public classThreadApiTest implements Runnable {

     

        private String type;

        private Object obj;

       

        ThreadApiTest(Objectobj){

            this.obj=obj;

        }

        /**

        *多线程相关api测试

        *@author : zhengrf1

         * @throws InterruptedException

        *@date创建时间:201737下午4:43:15

        */

        public static void main(String[] args) throws InterruptedException {

            // TODO Auto-generated method stub

    //      PipedInputStreamin = new PipedInputStream();

    //      PipedOutputStreamout = new PipedOutputStream();

            PipedReaderin = new PipedReader();

            PipedWriterout= newPipedWriter();

            ThreadApiTestinThread= newThreadApiTest(in);

            ThreadApiTestoutThread= newThreadApiTest(out);

            inThread.type="read";

            outThread.type="write";

            Threadintest= newThread(inThread);

            Threadouttest= newThread(outThread);

            try {

                in.connect(out);

            }catch(IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

     

            outtest.start();

            Thread.sleep(1000);

            intest.start();

        }

     

        /* @author : zhengrf1

         * @date 创建时间:201737下午4:46:48

         * @see java.lang.Runnable#run()

         */

        @Override

        public void run() {

            // TODO Auto-generated method stub

    //      while(true){

    //          System.out.println(Thread.currentThread().getName()+"isalive!");

    //          System.out.println(this.hashCode());

    //          synchronized(this){

    //          Thread.currentThread().isAlive();

                    Thread.sleep(100000);

    //          }

    //      }

            if(type.equals("write")){

                write();

            }

            else{

                read();

            }

        }

       

        public void write(){

            PipedWriterout= (PipedWriter)obj;

            try {

                out.write("hello");

            }catch(IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

       

        public void read(){

            PipedReaderin = (PipedReader)obj;

            try {

    //          byte[]b = new byte[50];

                char[] b = new char[50];

                System.out.println(in.read(b));

                Strings = new String(b);

                System.out.println(s);

            }catch(IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

     

    --值得注意的是,在java中父类转子类的形式是

    PipedWriter out = (PipedWriter)obj;

    然后再用out.write去操作

    而不是像C++那样强制转换就能使用(PipedWriter)obj.write

    另外一个知识点是byte[]String的方法不能直接

    byte[].toString();//这个实际只是上帝类的toString,里面是类名加哈希值

    而是使用String的构造方法创建出String对象String s = newString(b,"utf-8");

    转载请注明原文地址: https://ju.6miu.com/read-7511.html

    最新回复(0)