자바 다 중 스 레 드 간 통신

8826 단어 Java
          ,                ,          ,                      。

1.wait()와 notify()
1. wait()             ,          notify()  notifyAll()  。

                monitor,  lock,   。 
  
      wait()  ,         ,             (      notify()  notifyAll()  ),                   。
  
       wait()        , ,wait()         synchronized   synchronized  。
    
       :
        wait()   ,         。
  
               :Thread.sleep(),              ,                     。


2. notify()                  。

           ,             。        ,       。(                wait      )。

  **             ,                。**

                        ,       。    ,              ,       ,                    。

      wait()    ,notify        synchronized   synchronized  。
import java.util.ArrayList;
import java.util.List;

public class MyList {

    private static List list = new ArrayList();

    public static void add() {
        list.add("anyString");
    }

    public static int size() {
        return list.size();
    }
}


public class ThreadA extends Thread {

    private Object lock;

    public ThreadA(Object lock) {
        super();
        this.lock = lock;
    }

    @Override
    public void run() {
        try {
            synchronized (lock) {
                if (MyList.size() != 5) {
                    System.out.println("wait begin "
                            + System.currentTimeMillis());
                    lock.wait();
                    System.out.println("wait end  "
                            + System.currentTimeMillis());
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


public class ThreadB extends Thread {
    private Object lock;

    public ThreadB(Object lock) {
        super();
        this.lock = lock;
    }

    @Override
    public void run() {
        try {
            synchronized (lock) {
                for (int i = 0; i < 10; i++) {
                    MyList.add();
                    if (MyList.size() == 5) {
                        lock.notify();
                        System.out.println("       ");
                    }
                    System.out.println("   " + (i + 1) + "   !");
                    Thread.sleep(1000);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class Run {

    public static void main(String[] args) {

        try {
            Object lock = new Object();

            ThreadA a = new ThreadA(lock);
            a.start();

            Thread.sleep(50);

            ThreadB b = new ThreadB(lock);
            b.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
  A          (list.size()==5),     。  B  list     ,  list  size。

A,B        ?    ,  A     list.size()    5  ?

     Object   wait()   notify()   。

       (list.size() !=5),  A  wait()   CPU,       。

      ,  B   notify()     A,      A,      A,          。

2.synchronized 키워드
public class MyObject {

    synchronized public void methodA() {
        //do something....
    }

    synchronized public void methodB() {
        //do some other thing
    }
}

public class ThreadA extends Thread {

    private MyObject object;
//      
    @Override
    public void run() {
        super.run();
        object.methodA();
    }
}

public class ThreadB extends Thread {

    private MyObject object;
//      
    @Override
    public void run() {
        super.run();
        object.methodB();
    }
}

public class Run {
    public static void main(String[] args) {
        MyObject object = new MyObject();

        //  A   B          :object
        ThreadA a = new ThreadA(object);
        ThreadB b = new ThreadB(object);
        a.start();
        b.start();
    }
}
    A   B     MyObject    object,                ,          ,  :  B      A    methodA()    ,     methodB()  。  ,  A   B      。

    ,     “    ”    。               ,     (       ),      。

좋은 웹페이지 즐겨찾기