實現一個容器,提供兩個方法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();
}
}