多线程面试题一:
实现一个容器,提供两个方法,add,size
写两个线程,线程1添加10个元素到容器中,线程2实现监控元素的个数,当个数到5个时,线程2给出提示并结束
方法一:
• park unpark ( LockSupport)
• semaphore 灯塔
public class thread_two_thread_01 {
List<Integer> list = new ArrayList<>();
public static void main(String[] args) {
thread_two_thread_01 t = new thread_two_thread_01();
Semaphore semaphore = new Semaphore(1);
Thread t2 = new Thread(() -> {
LockSupport.park();
System.out.println("哈哈哈 五个了 我继续执行了");
try {
semaphore.acquire();
System.out.println("我开始执行了,你等我三秒钟");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
semaphore.release();
});
Thread t1 = new Thread(() -> {
for (int i = 0; i < 10; i++) {
t.list.add(i);
System.out.println("开始执行"+i);
try {
Thread.sleep(1000);
if(t.list.size() == 5){
LockSupport.unpark(t2);
Thread.sleep(1);
semaphore.acquire();
System.out.println("我这边五个了");
}
} catch (InterruptedException e) {
}
}
semaphore.release();
});
t1.start();
t2.start();
}
}
方法二:
• semaphore 灯塔
• join 线程
public class thread_two_thread_02 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
Semaphore semaphore = new Semaphore(1);
Thread t2 = new Thread(() -> {
try {
semaphore.acquire();
System.out.println("哈哈哈 五个了 我继续执行了");
System.out.println("我开始执行了,你等我三秒钟");
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
semaphore.release();
});
Thread t1 = new Thread(() -> {
try {
semaphore.acquire();
for (int i = 0; i < 10; i++) {
System.out.println("开始执行" + i);
if (i == 5) {
System.out.println("开始等待");
semaphore.release();
t2.join();
}
list.add(i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
t1.start();
t2.start();
}
}
方法三:
- countDownLatch (线程门栓)
- join 线程
public class thread_two_thread_03 {
volatile List<Integer> list = new ArrayList<>();
public static void main(String[] args) {
thread_two_thread_03 t = new thread_two_thread_03();
CountDownLatch countDownLatch = new CountDownLatch(1);
Thread t1 = new Thread(() -> {
try{
System.out.println("开始等待了");
countDownLatch.await();
System.out.println("到我了,你等我两秒钟");
Thread.sleep(2000);
}catch (Exception e){
}
});
Thread t2 = new Thread(() -> {
try{
System.out.println("我先开始执行");
for (int i = 0; i < 10; i++) {
Thread.sleep(200);
System.out.println(i);
if(i == 5){
countDownLatch.countDown();
t1.join();
}
}
}catch (Exception e){
}
});
t1.start();
t2.start();
}
}
方法四:
- synchronized
- wait
- notify
- notifyAll
- 特备注意: wait+notify 都是跟synchronized配合使用的
- o.notify o.wait 必须在含有锁的o对象上
public class Thread_two_thread_04 {
public static void main(String[] args) {
Object obj = new Object();
new Thread(()->{
try{
System.out.println("我先来");
synchronized (obj){
System.out.println("我抢到锁了");
obj.wait();
System.out.println("终于到我了");
Thread.sleep(1000);
System.out.println("你给我等一秒钟");
obj.notify();
}
}catch (Exception e){
System.err.println(e);
}
System.out.println("是这里吗");
}).start();
try{
Thread.sleep(1);
}catch (Exception e){
System.out.println(e);
}
new Thread(()->{
try{
synchronized (obj){
for (int i = 0; i < 10; i++) {
Thread.sleep(200);
System.out.println(i);
if(i == 5){
obj.notify();
obj.wait();
System.out.println("我继续执行");
}
}
}
}catch (Exception e){
System.out.println(e);
}finally {
}
}).start();
}
}