java并发编程面试题1

实现一个容器,提供两个方法add和size
写两个线程,线程1添加十个元素到容器中,线程2实现监控元素的个数,当个数达到五个时线程2 给出提示并结束

方法一:volatile关键字

public class MyContainer1 {
	volatile List list = new ArrayList();
	
	public void add(Object o) {
		list.add(o);
	}
	
	public int size() {
		return list.size();
	}
	
	
	public static void main(String[] args) {
		
		MyContainer1 container1 = new MyContainer1();
		
		new Thread(() -> {
			for(int i = 0; i < 10; i++) {
				container1.add(new Object());
				System.err.println("add" + i);
				
				try {
					TimeUnit.SECONDS.sleep(1);
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
			}
		}, "t1").start();
		
		new Thread(() -> {
			while(true)
				if(container1.size() == 5)
					break;
			System.err.println("t2 end");
		}, "t2").start();
	}
}

缺点:
while循环太费CPU

2.使用wait和notify方法<这种方法不行,这里只是做一个演示>

  • wait方法会释放锁
  • notify不会释放锁
public class MyContainer2 {
	volatile List list = new ArrayList();
	
	public void add(Object o) {
		list.add(o);
	}
	
	public int size() {
		return list.size();
	}
	
	
	public static void main(String[] args) {
		
		MyContainer2 container1 = new MyContainer2();
		
		final Object lock = new Object();
		
		new Thread(() -> {
			synchronized (lock) {
				System.out.println("t2 start");
				
				if(container1.size() != 5) {
					try {
						lock.wait();
					} catch (InterruptedException e) {
						// TODO: handle exception
					}
				}
				System.out.println("t2 end...");
			}
				
		}, "t2").start();
		
		new Thread(() -> {
			synchronized (lock) {
				for(int i = 0; i < 10; i++) {
					container1.add(new Object());
					System.err.println("add" + i);
					
					if (container1.size() == 5) {
						lock.notify();//会唤醒线程,但是不会释放锁,所以t2还是不会有锁,所以还是会出错
					}
					try {
						TimeUnit.SECONDS.sleep(1);
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
			}
		}, "t1").start();
		
		
	}
}
调整如下:
public class MyContainer3 {
	volatile List list = new ArrayList();
	
	public void add(Object o) {
		list.add(o);
	}
	
	public int size() {
		return list.size();
	}
	
	
	public static void main(String[] args) {
		
		MyContainer3 container1 = new MyContainer3();
		
		final Object lock = new Object();
		
		new Thread(() -> {
			synchronized (lock) {
				System.out.println("t2 start");
				
				if(container1.size() != 5) {
					try {
						lock.wait();
					} catch (InterruptedException e) {
						// TODO: handle exception
					}
				}
				System.out.println("t2 end...");

				lock.notify();
			}
				
		}, "t2").start();
		
		new Thread(() -> {
			synchronized (lock) {
				for(int i = 0; i < 10; i++) {
					container1.add(new Object());
					System.err.println("add" + i);
					
					if (container1.size() == 5) {
						lock.notify();//会唤醒线程,但是不会释放锁,所以t2还是不会有锁
						
						try {
							lock.wait();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
					try {
						TimeUnit.SECONDS.sleep(1);
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
			}
		}, "t1").start();
	}
}
方法三:
  • 使用latch中的await和countdown来替代wait和notify
  • 好处是:通信简单,同时可以指定等待的时间
  • CountDownLatch不涉及锁定,当count值为0 时当前的线程继续执行
  • 这并不涉及同步,只是在线程通信的过程中synchronized + wait/notify 显得太重了
public class MyContainer4 {
	volatile List list = new ArrayList();
	
	public void add(Object o) {
		list.add(o);
	}
	
	public int size() {
		return list.size();
	}
	
	
	public static void main(String[] args) {
		
		MyContainer4 container1 = new MyContainer4();
		
		CountDownLatch latch = new CountDownLatch(1);
		
		new Thread(() -> {
				System.out.println("t2 start");
				
				if(container1.size() != 5) {
					try {
						latch.await();
					} catch (InterruptedException e) {
						// TODO: handle exception
					}
				}
				System.out.println("t2 end...");			
				
		}, "t2").start();
		
		new Thread(() -> {
				for(int i = 0; i < 10; i++) {
					container1.add(new Object());
					System.err.println("add" + i);
					
					if (container1.size() == 5) {
						latch.countDown();
					}
					try {
						TimeUnit.SECONDS.sleep(1);
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
				}
			
		}, "t1").start();
		
		
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值