线程间通信

来源:互联网 发布:pack php java 编辑:程序博客网 时间:2024/05/21 14:02
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++;    }}


0 0
原创粉丝点击