线程间通信

1.多线程间使用共享变量
public class CommunicateByShareVaria {


    public static void main(String[] args) {
        InnerShareVaria innerShareVaria = new InnerShareVaria();
        Thread thread = new Thread(innerShareVaria.getMyThread());
        Thread thread1 = new Thread(innerShareVaria.getMyThread());
        Thread threa2 = new Thread(innerShareVaria.getMyThread());
        thread1.start();
        threa2.start();
        thread.start();

    }


}


class InnerShareVaria {
    private int flag;

    class MyThread implements Runnable {
        public synchronized void run() {//由于为内部类,类被类加载器加载的时候,就会在内存中保留一份数据,所以要对run()方法加锁,实现数据flag同步
            for (int i = 0; i < 50; i++) {
                System.out.println(Thread.currentThread().getName() + " is running and flag is " + flag++);
            }
        }
    }

    public Runnable getMyThread() {
        return new MyThread(); //保证每个线程都不一样
    }
}

2.通过pipe管道流,将生产者线程与消费者线程连接起来

public class CommunicateByPipe {
    public static void main(String[] args) {
        try {
            PipedInputStream pipedInputStream = new PipedInputStream();
            PipedOutputStream pipedOutputStream = new PipedOutputStream();
            Producer producer = new Producer(pipedOutputStream);
            Consumer consumer = new Consumer(pipedInputStream);

            pipedInputStream.connect(pipedOutputStream);//输出、输入流管道连接,相当于producer与consumer相连

            Thread thread = new Thread(producer);
            Thread thread1 = new Thread(consumer);
            thread.start();
            thread1.start();


        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }


}

class Producer implements Runnable {
    private PipedOutputStream pipedOutputStream;

    public Producer(PipedOutputStream pipedOutputStream) {
        this.pipedOutputStream = pipedOutputStream;
    }

    public void run() {
        try {
            Thread.sleep(5000);
            String data = "producer往管道中开始写数据了";
            pipedOutputStream.write(data.getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

class Consumer implements Runnable {
    private PipedInputStream pipedInputStream;

    public Consumer(PipedInputStream pipedInputStream) {
        this.pipedInputStream = pipedInputStream;
    }

    public void run() {
        System.out.println("consumer is waiting data from data");
        try {
            int byteNum = pipedInputStream.read();
            System.out.println("consumer read " + byteNum + "b ytes data from  pipe");
        } catch (IOException e) {


        }
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

3.通过线程A调用线程B方法,对线程B变量做处理

public class CommunicateByCommonInterface {
    public static void main(String[] args) {
        ThreadB threadB = new ThreadB();
        ThreadA threadA = new ThreadA(threadB);

        Thread thread = new Thread(threadA);
        Thread thread2 = new Thread(threadA);
        Thread thread3 = new Thread(threadA);
        thread.start();
        thread2.start();
        thread3.start();

    }


}


class ThreadA implements Runnable {
    private ThreadB threadB;

    public ThreadA(ThreadB threadB) {
        this.threadB = threadB;
    }

    public void run() {

        for (int i = 0; i < 20; ++i) {
            System.out.println(Thread.currentThread().getName() + " running flag is " );
            System.out.println(threadB.getFlag());
        } //必须等线程ThreadB方法getFlag()方法返回才能继续其他操作
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

class ThreadB implements Runnable {
    public int flag;

    public void run() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public int getFlag() {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return flag++;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值