多线程面试题
实现一个容器,提供两个方法,add,size;写两个线程,线程1添加10个元素到容器中,线程2实现监控元素的个数,当个数到5个时,线程2给出提示并结束
1.wait和notify实现
private static List<String> list = new ArrayList<>();
public static void main(String[] args) {
Object locak = new Object();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (locak){
if(list.size()!=5){
try {
locak.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("监测到容器长度为5,线程2结束...");
locak.notify();//唤醒其他等锁对象
}
}
});
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (locak){
for (int i = 0; i <10 ; i++) {
System.out.println("添加对象" + i);
list.add(String.valueOf(i));
if(list.size()==5){
locak.notify();
try {
locak.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("线程1结束...");
}
}
});
thread.start();
thread1.start();
}
2.LockSupport实现
private static List<String> list = new ArrayList<>();
static Thread t1 =null,t2 = null;
public static void main(String[] args) {
t1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
System.out.println("添加对象" + i);
list.add(String.valueOf(i));
if(list.size()==5){
LockSupport.unpark(t2);
LockSupport.park();
}
}
System.out.println("线程1结束...");
}
});
t2 = new Thread(new Runnable() {
@Override
public void run() {
LockSupport.park();
System.out.println("t2 结束");
LockSupport.unpark(t1);
System.out.println("线程2结束。。。。");
}
});
t1.start();
t2.start();
}
3.Semaphore实现
public static void main(String[] args) {
Semaphore semaphoreA = new Semaphore(0);
Semaphore semaphoreB = new Semaphore(0);
Container container = new Container();
ThreadA threadA = new ThreadA(container,semaphoreA,semaphoreB);
Thread thread = new Thread(threadA);
ThreadB threadB = new ThreadB(semaphoreA,semaphoreB);
Thread thread1 = new Thread(threadB);
thread.start();
thread1.start();
}
static class Container {
private List<Integer> list = new ArrayList<>();
public void add(int a){
list.add(a);
}
public int size(){
return list.size();
}
}
static class ThreadA implements Runnable {
private Container container;
private Semaphore semaphoreA;
private Semaphore semaphoreB;
public ThreadA(Container container, Semaphore semaphoreA, Semaphore semaphoreB) {
this.container = container;
this.semaphoreA = semaphoreA;
this.semaphoreB = semaphoreB;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
if(container.size()==5){
semaphoreB.release();
try {
semaphoreA.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("添加数据是"+i);
container.add(i);
}
}
}
static class ThreadB implements Runnable{
private Semaphore semaphoreA;
private Semaphore semaphoreB;
public ThreadB(Semaphore semaphoreA, Semaphore semaphoreB) {
this.semaphoreA = semaphoreA;
this.semaphoreB = semaphoreB;
}
@Override
public void run() {
try {
semaphoreB.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程b结束");
semaphoreA.release();
}
}
4.CuntDownLatch实现
public static void main(String[] args) {
MyContainer myContainer = new MyContainer();
CountDownLatch countDownLatch = new CountDownLatch(1);
CountDownLatch countDownLatch1 = new CountDownLatch(1);
ThreadA threadA = new ThreadA(countDownLatch,countDownLatch1,myContainer);
ThreadB threadB = new ThreadB(countDownLatch,countDownLatch1);
Thread thread = new Thread(threadA);
Thread thread1 = new Thread(threadB);
thread.start();
thread1.start();
}
static class MyContainer{
List<Integer> list = new ArrayList<>();
public void add(int a){
list.add(a);
}
public int size(){
return list.size();
}
}
static class ThreadA implements Runnable{
CountDownLatch countDownLatchA;
CountDownLatch countDownLatchB;
MyContainer myContainer;
public ThreadA(CountDownLatch countDownLatchA, CountDownLatch countDownLatchB, MyContainer myContainer) {
this.countDownLatchA = countDownLatchA;
this.countDownLatchB = countDownLatchB;
this.myContainer = myContainer;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
if(myContainer.size()==5){
try {
countDownLatchB.countDown();
countDownLatchA.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("添加数据是"+i);
myContainer.add(i);
}
System.out.println("线程a执行完毕");
}
}
static class ThreadB implements Runnable{
CountDownLatch countDownLatchA;
CountDownLatch countDownLatchB;
public ThreadB(CountDownLatch countDownLatchA, CountDownLatch countDownLatchB) {
this.countDownLatchA = countDownLatchA;
this.countDownLatchB = countDownLatchB;
}
@Override
public void run() {
try {
countDownLatchB.await();
System.out.println("线程b执行完毕");
countDownLatchA.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
5.CyclicBarrier实现
public static void main(String[] args) {
ThreadLatch.MyContainer myContainer = new ThreadLatch.MyContainer();
CyclicBarrier cyclicBarrier = new CyclicBarrier(1, new Runnable() {
@Override
public void run() {
System.out.println("线程b执行完成");
}
});
ThreadA threadA= new ThreadA(cyclicBarrier,myContainer);
Thread thread = new Thread(threadA);
thread.start();
}
static class MyContainer{
List<Integer> list = new ArrayList<>();
public void add(int a){
list.add(a);
}
public int size(){
return list.size();
}
}
static class ThreadA implements Runnable{
CyclicBarrier cyclicBarrierA;
ThreadLatch.MyContainer myContainer;
public ThreadA(CyclicBarrier cyclicBarrierA, ThreadLatch.MyContainer myContainer) {
this.cyclicBarrierA = cyclicBarrierA;
this.myContainer = myContainer;
}
@Override
public void run() {
for (int i = 0; i <10 ; i++) {
if(myContainer.size()==5){
try {
cyclicBarrierA.await();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("添加数据是"+i);
myContainer.add(i);
}
System.out.println("线程a执行完毕");
}
}