淘宝面试题之CountDownLatch应用
来源:互联网 发布:淘宝设置经营地址无效 编辑:程序博客网 时间:2024/06/05 21:53
XML Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.lyzx.day01;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* 曾经的面试题:(淘宝?)
* 实现一个容器,提供两个方法 add,size
* 写两个线程,线程1添加10个元素到容器中,线程2实现监控元素的个数,当个数到5个时,线程2给出提示并结束
*
*/
public class T5{
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(1);
Arr<Object> arr = new Arr<Object>(latch);
new Thread(new R2(latch)).start();
new Thread(new R1(arr)).start();
}
}
class Arr<E>{
private CountDownLatch latch;
private List<E> arr = new ArrayList<E>();
public Arr(CountDownLatch latch){
this.latch=latch;
}
public synchronized boolean add(E e){
boolean v = arr.add(e);
try {
Thread.sleep(300);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
if(5 == size()){
latch.countDown();
}
System.out.println("======"+size());
return v;
}
public synchronized int size(){
return arr.size();
}
}
class R1 implements Runnable{
private Arr<Object> arr;
public R1(Arr<Object> arr){
this.arr=arr;
}
@Override
public void run(){
for(int i=0;i<10;i++){
arr.add(new Object());
}
}
}
class R2 implements Runnable{
private CountDownLatch latch;
public R2(CountDownLatch latch){
this.latch= latch;
}
@Override
public void run(){
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("now 。。。。。over..");
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* 曾经的面试题:(淘宝?)
* 实现一个容器,提供两个方法 add,size
* 写两个线程,线程1添加10个元素到容器中,线程2实现监控元素的个数,当个数到5个时,线程2给出提示并结束
*
*/
public class T5{
public static void main(String[] args) {
CountDownLatch latch = new CountDownLatch(1);
Arr<Object> arr = new Arr<Object>(latch);
new Thread(new R2(latch)).start();
new Thread(new R1(arr)).start();
}
}
class Arr<E>{
private CountDownLatch latch;
private List<E> arr = new ArrayList<E>();
public Arr(CountDownLatch latch){
this.latch=latch;
}
public synchronized boolean add(E e){
boolean v = arr.add(e);
try {
Thread.sleep(300);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
if(5 == size()){
latch.countDown();
}
System.out.println("======"+size());
return v;
}
public synchronized int size(){
return arr.size();
}
}
class R1 implements Runnable{
private Arr<Object> arr;
public R1(Arr<Object> arr){
this.arr=arr;
}
@Override
public void run(){
for(int i=0;i<10;i++){
arr.add(new Object());
}
}
}
class R2 implements Runnable{
private CountDownLatch latch;
public R2(CountDownLatch latch){
this.latch= latch;
}
@Override
public void run(){
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("now 。。。。。over..");
}
}
阅读全文
0 0
- 淘宝面试题之CountDownLatch应用
- 淘宝面试题
- 淘宝面试题
- 淘宝面试题cacheline
- 淘宝面试题
- 几行代码解决淘宝面试题之Clojure版
- 淘宝的一道面试题
- 一道淘宝校园面试题
- 淘宝的一道面试题
- 淘宝研究院的面试题
- 淘宝面试题--大表建约束
- 剑指Offer之淘宝面试题之小白鼠与毒药解题过程分析
- 一道淘宝校园面试题的思考
- 淘宝网前端开发面试题
- 淘宝面试题 放鸡蛋问题
- 淘宝UED前端开发面试题
- 淘宝面试题猜数字游戏
- 淘宝某部门的技术面试题
- JS基础学习第六天:JavaScript对象入门(构造函数和实例对象)
- 切换选项卡+js+css+html
- python实现pat 1054. The Dominant Color (20)
- Android.mk详解
- http content-type常见文件格式类型
- 淘宝面试题之CountDownLatch应用
- java基础(5)- 面向对象(2)
- 射频了解一丢丢【3】
- 665. Non-decreasing Array
- bzoj1008 越狱【动态规划+快速幂】
- [AHOI2009]中国象棋
- 史上最全的maven pom.xml文件教程详解
- String类的学习和使用
- Tv开发初体验 焦点移动