实现一个容器,提供两个方法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();
}
}