面试题

满足一下条件

  • 定义一个容器 提供添加元素和获取元素数量的 方法
  • 启动两个线程 第一个添加元素,第二个获取元素,当第二个获取的元素的长度为5时 结束这个线程

1,使用 volatile

public class Test {
    
    public static void main(String[] args) {
        
        Container container = new Container();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    container.add(new Object());
                    System.out.println("add Object - "+ i);
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    if(container.size()==5) {
                        System.out.println("t2 size() : 5");
                        break;  
                    }   
                }
            }
        }).start();
    }

}

class Container {
    volatile List container  = new ArrayList();
    
    public void add(Object o) {
        container.add(o);
    }
    
    public int size() {
        return container.size();
    }
}
 
 

2 ,使用CountDownLatch

public class Test {
    public static void main(String[] args) {
        Container container = new Container(); 
        CountDownLatch latch = new CountDownLatch(1);
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    container.add(new Object());
                    System.out.println("add Object() - "+ i);
                    if(container.size()==5) {
                        latch.countDown(); //减去一个门闩
                    }
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                
            }
        }).start();
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                if(container.size()!=5) {
                    try {
                        latch.await();  //等待门闩开放
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("t2 size() 5");
            }
        }).start();
    }
}

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

3,wait() 和notifyAll()

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

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

                            
                        
                    
                    
                    

你可能感兴趣的:(面试题)