wait¬ify

/**
 * wait notify
 */
package com.sxt.concurrent.t02;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Test_02 {
    public static void main(String[] args) {
        final Test_02_Container t = new Test_02_Container();
        final Object lock = new Object();
        
        new Thread(new Runnable(){
            @Override
            public void run() {
                synchronized (lock) {
                    if(t.size() != 5){
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("size = 5");
                }
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    for(int i = 0; i < 10; i++){
                        System.out.println("add Object to Container " + i);
                        t.add(new Object());
                        if(t.size() == 5){
                            lock.notifyAll();
                        }
                        try {
                            TimeUnit.SECONDS.sleep(1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();
    }
}

class Test_02_Container{
    List container = new ArrayList<>();
    
    public void add(Object o){
        this.container.add(o);
    }
    
    public int size(){
        return this.container.size();
    }
}
 

                            
                        
                    
                    
                    

你可能感兴趣的:(wait¬ify)