导航

Java 线程间通讯(共享变量方式)

Posted on 2013-06-27 19:27  寒宵飞飞  阅读(823)  评论(0编辑  收藏  举报

Java线程间通讯,最常用的方式便是共享变量方式,多个线程共享一个静态变量就可以实现在线程间通讯,但是这需要注意的就是线程同步问题。

一、没考虑线程同步:

package com.wyf;

public class threadConnetcion {
    public static void main(String[] args) {

        Q q=new Q();
        //创建生产者线程
        Producer p = new Producer(q);
        
         //创建消费者线程
        Consumer c = new Consumer(q);
        /**
         * 启动线程
         */
        p.start();
        c.start();
    }

}

//生产者线程
class Producer extends Thread { Q q; public Producer(Q q) { this.q = q; } public void run() { try { int i=0; while(true) { this.sleep(3000); q.put(i++); } } catch (Exception e) { e.printStackTrace(); } } } //消费者线程
class Consumer extends Thread { Q q; public Consumer(Q q) { this.q = q; } public void run() { try { while(true) { this.sleep(3000); q.get(); } } catch (Exception e) { e.printStackTrace(); } } } class Q { int n; synchronized int get() { System.out.println("Get:"+n); return n; } synchronized void put(int n) { this.n=n; System.out.println("Put:"+n); } }

输出如下:

Put:0
Get:0
Get:0
Put:1
Put:2
Get:2
Get:2
Put:3
Get:3
Put:4
Put:5
Get:5
Put:6
Get:6
Get:6
Put:7
Put:8
Get:8

可以看到线程之间的通讯是杂乱的;

 

 

二、使用wait和notify进行线程同步:

package com.wyf;

public class threadConnetcion {
    public static void main(String[] args) {

        Q q=new Q();
        //创建生产者线程
        Producer p = new Producer(q);
        
         //创建消费者线程
        Consumer c = new Consumer(q);
        /**
         * 启动线程
         */
        p.start();
        c.start();
    }

}

//生产者线程
class Producer extends Thread { Q q; public Producer(Q q) { this.q = q; } public void run() { try { int i=0; while(true) { this.sleep(3000); q.put(i++); } } catch (Exception e) { e.printStackTrace(); } } } //消费者线程
class Consumer extends Thread { Q q; public Consumer(Q q) { this.q = q; } public void run() { try { while(true) { this.sleep(3000); q.get(); } } catch (Exception e) { e.printStackTrace(); } } } class Q { int n; boolean valueSet=false; synchronized int get() { while(!valueSet) { try { wait(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("Get:"+n); valueSet=false; notify(); return n; } synchronized void put(int n) { while(valueSet) { try { wait(); } catch (Exception e) { e.printStackTrace(); } } this.n=n; System.out.println("Put:"+n); valueSet=true; notify(); } }

输出如下:

Put:0
Get:0
Put:1
Get:1
Put:2
Get:2