java線程之間的通訊

來源:互聯網
上載者:User

標籤:擷取   一個   exchange   await   code   ace   tac   div   通訊   

1.常用的方法
  sleep()該線程進入等待狀態,不釋放鎖
  wait() 該線程進入等待狀態,釋放鎖
  notify() 隨機喚醒一個線程
  notifyAll() 喚醒全部線程

2.線程之間的通訊

  a.兩個線程之間的通訊

  

public class ThreadExchange {@Testpublic void test2Thread() {MyPrint myPrint = new MyPrint();new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {myPrint.printA();} catch (InterruptedException e) {e.printStackTrace();}}}}).start();new Thread(new Runnable() {@Overridepublic void run() {while (true) {try {myPrint.printB();} catch (InterruptedException e) {e.printStackTrace();}}}}).start();}}class MyPrint {private Integer flag = 0;public synchronized void printA() throws InterruptedException {if (flag != 0) {this.wait();}System.out.print("a");System.out.print("a");System.out.print("a");System.out.print("a");System.out.println("a");flag = 1;this.notify();}public synchronized void printB() throws InterruptedException {if (flag != 1) {this.wait();}System.out.print("b");System.out.print("b");System.out.print("b");System.out.print("b");System.out.println("b");flag = 0;this.notify();}}

  b.三個以上的線程之間的通訊

   方式一

   

public class ThreadExchange {    @Test    public void test2Thread() {        MyPrint myPrint = new MyPrint();        new Thread(new Runnable() {            @Override            public void run() {                while (true) {                    try {                        myPrint.printA();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }).start();        new Thread(new Runnable() {            @Override            public void run() {                while (true) {                    try {                        myPrint.printB();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }).start();    }}class MyPrint {    private Integer flag = 0;    public synchronized void printA() throws InterruptedException {        if (flag != 0) {            this.wait();        }        System.out.print("a");        System.out.print("a");        System.out.print("a");        System.out.print("a");        System.out.println("a");        flag = 1;        this.notify();    }    public synchronized void printB() throws InterruptedException {        if (flag != 1) {            this.wait();        }        System.out.print("b");        System.out.print("b");        System.out.print("b");        System.out.print("b");        System.out.println("b");        flag = 0;        this.notify();    }}            2.三個以上的線程之間的通訊public class ThreadExchange {    @Test    public void test2Thread() {        final MyPrint p = new MyPrint();        new Thread() {            public void run() {                while (true) {                    try {                        p.print1();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }.start();        new Thread() {            public void run() {                while (true) {                    try {                        p.print2();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }.start();        new Thread() {            public void run() {                while (true) {                    try {                        p.print3();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }.start();    }}class MyPrint {    private int flag = 1;    public void print1() throws InterruptedException {                                    synchronized(this) {            while(flag != 1) {                this.wait();                                }            System.out.print("a");            System.out.print("a");            System.out.print("a");            System.out.print("a");            System.out.println("a");            flag = 2;            this.notifyAll();        }    }    public void print2() throws InterruptedException {        synchronized (this) {            while (flag != 2) {                this.wait();            }            System.out.print("b");            System.out.print("b");            System.out.print("b");            System.out.print("b");            System.out.println("b");            flag = 3;            this.notifyAll();        }    }    public void print3() throws InterruptedException {        synchronized (this) {            while (flag != 3) {                this.wait();            }            System.out.print("c");            System.out.print("c");            System.out.print("c");            System.out.print("c");            System.out.println("c");            flag = 1;            this.notifyAll();        }    }}

    方式二

    public class ThreadExchange {    @Test    public void test3Thread() {        final MyPrint p = new MyPrint();        new Thread() {            public void run() {                while (true) {                    try {                        p.print1();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }.start();        new Thread() {            public void run() {                while (true) {                    try {                        p.print2();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }.start();        new Thread() {            public void run() {                while (true) {                    try {                        p.print3();                    } catch (InterruptedException e) {                        e.printStackTrace();                    }                }            }        }.start();    }}class MyPrint {    private ReentrantLock r = new ReentrantLock();    private Condition c1 = r.newCondition();    private Condition c2 = r.newCondition();    private Condition c3 = r.newCondition();    private int flag = 1;    public void print1() throws InterruptedException {        r.lock(); // 擷取鎖        if (flag != 1) {            c1.await();        }        System.out.print("a");        System.out.print("a");        System.out.print("a");        System.out.print("a");        System.out.println("a");        flag = 2;        c2.signal();        r.unlock(); // 釋放鎖    }    public void print2() throws InterruptedException {        r.lock();        if (flag != 2) {            c2.await();        }        System.out.print("b");        System.out.print("b");        System.out.print("b");        System.out.print("b");        System.out.println("b");        flag = 3;        c3.signal();        r.unlock();    }    public void print3() throws InterruptedException {        r.lock();        if (flag != 3) {            c3.await();        }        System.out.print("c");        System.out.print("c");        System.out.print("c");        System.out.print("c");        System.out.println("c");        flag = 1;        c1.signal();        r.unlock();    }}

 

java線程之間的通訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.