zoukankan      html  css  js  c++  java
  • java线程(2)-线程间通信

    方法一 通过访问共享变量的方式(注:需要处理同步问题) 
    方法二 通过管道流

    其中方法一有两种实现方法,即 
    方法一a)通过内部类实现线程的共享变量 

    public class Innersharethread {     
        public static void main(String[] args) {     
            Mythread mythread = new Mythread();     
            mythread.getThread().start();     
            mythread.getThread().start();     
            mythread.getThread().start();     
            mythread.getThread().start();     
        }     
    }     
    class Mythread {     
        int index = 0;     
         
        private class InnerThread extends Thread {     
            public synchronized void run() {     
                while (true) {     
                    System.out.println(Thread.currentThread().getName()     
                            + "is running and index is " + index++);     
                }     
            }     
        }     
         
        public Thread getThread() {     
            return new InnerThread();     
        }     
    }    
       
    /** 
     * 通过内部类实现线程的共享变量 
     * 
     */ 
    public class Innersharethread {  
        public static void main(String[] args) {  
            Mythread mythread = new Mythread();  
            mythread.getThread().start();  
            mythread.getThread().start();  
            mythread.getThread().start();  
            mythread.getThread().start();  
        }  
    }  
    class Mythread {  
        int index = 0;  
       
        private class InnerThread extends Thread {  
            public synchronized void run() {  
                while (true) {  
                    System.out.println(Thread.currentThread().getName()  
                            + "is running and index is " + index++);  
                }  
            }  
        }  
       
        public Thread getThread() {  
            return new InnerThread();  
        }  
    }
    View Code

    b)通过实现Runnable接口实现线程的共享变量 

    public class Interfacaesharethread {
        public static void main(String[] args) {
            Mythread mythread = new Mythread();
            new Thread(mythread).start();
            new Thread(mythread).start();
            new Thread(mythread).start();
            new Thread(mythread).start();
        }
    }
     
    /* 实现Runnable接口 */
    class Mythread implements Runnable {
        int index = 0;
     
        public synchronized void run() {
            while (true)
                System.out.println(Thread.currentThread().getName() + "is running and
                            the index is " + index++);
        }
    }
     
    /**
     * 通过实现Runnable接口实现线程的共享变量
      
     */
    public class Interfacaesharethread {
        public static void main(String[] args) {
            Mythread mythread = new Mythread();
            new Thread(mythread).start();
            new Thread(mythread).start();
            new Thread(mythread).start();
            new Thread(mythread).start();
        }
    }
     
    /* 实现Runnable接口 */
    class Mythread implements Runnable {
        int index = 0;
     
        public synchronized void run() {
            while (true)
                System.out.println(Thread.currentThread().getName() + "is running and
                            the index is " + index++);
        }
    }
    View Code

    方法二(通过管道流): 

    public class CommunicateWhitPiping {
        public static void main(String[] args) {
            /**
             * 创建管道输出流
             */
            PipedOutputStream pos = new PipedOutputStream();
            /**
             * 创建管道输入流
             */
            PipedInputStream pis = new PipedInputStream();
            try {
                /**
                 * 将管道输入流与输出流连接 此过程也可通过重载的构造函数来实现
                 */
                pos.connect(pis);
            } catch (IOException e) {
                e.printStackTrace();
            }
            /**
             * 创建生产者线程
             */
            Producer p = new Producer(pos);
            /**
             * 创建消费者线程
             */
            Consumer c = new Consumer(pis);
            /**
             * 启动线程
             */
            p.start();
            c.start();
        }
    }
     
    /**
     * 生产者线程(与一个管道输入流相关联)
     * 
     */
    class Producer extends Thread {
        private PipedOutputStream pos;
     
        public Producer(PipedOutputStream pos) {
            this.pos = pos;
        }
     
        public void run() {
            int i = 8;
            try {
                pos.write(i);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
     
    /**
     * 消费者线程(与一个管道输入流相关联)
     * 
     */
    class Consumer extends Thread {
        private PipedInputStream pis;
     
        public Consumer(PipedInputStream pis) {
            this.pis = pis;
        }
     
        public void run() {
            try {
                System.out.println(pis.read());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    View Code
  • 相关阅读:
    软件破解系列之OD中断方法
    C#获取类名为Internet_Explorer_Server控件的内容
    C#重启网卡(支持xp和win7)
    软件开发专业技术名词的解释
    一些软件设计的原则
    AntlrWorks的Open没有反应,安装JDK6即可解决
    向大家推荐《思考中医》,这本书对我来说算是中医启蒙了 无为而为
    面对富裕对于贫穷的几乎是天生傲慢我们要呐喊: 我们并不笨,我们只是穷读新科诺贝尔文学奖得主,奥尔罕·帕慕克作品有感 无为而为
    推荐一本书《罗伯特议事规则(Robert's Rules of Order) 》,也许对于想要参与管理的技术人员来说是很有用的 无为而为
    再次发布一个工作地点在上海的Biztalk专家招聘 无为而为
  • 原文地址:https://www.cnblogs.com/arbitrary/p/5263634.html
Copyright © 2011-2022 走看看