栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA 多线程面试题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 多线程面试题

线程的状态

参考Java线程的6种状态及切换(透彻讲解)

线程池

线程池参数 老八股文了。俩个时间参数是 线程空闲回收时间,回收时候用的。
execute 提交时候的流程 老八股文了。
线程回收条件

while (task != null || (task = getTask()) != null) {
//task  为null 会去 拿线程执行,拿不到就回收。
}
拿不到的条件。
1、队列空。线程池停了等情况
if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }
2、 (工作线程大于最小线程 || 超过空闲时间)&&(工作线程大于1 || 队列空)
工作线程大于1 是应为自己停了怎么也得有个在运行的,队列空就不需要了
 int wc = workerCountOf(c);
// Are workers subject to culling?
boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;
if ((wc > maximumPoolSize || (timed && timedOut))
                && (wc > 1 || workQueue.isEmpty())) {
                if (compareAndDecrementWorkerCount(c))
                    return null;
                continue;
            }
笔试题 ab俩个线程交替打印,

可以取个巧直接用synchronized wait notifyAll

Object o = new Object();
        AtomicInteger count = new AtomicInteger(0);
        new Thread(new P("a", o, count),"aaa").start();
        new Thread(new P("b", o, count),"bbb").start();

static class P implements Runnable{
        String info;
        final Object lock;
        AtomicInteger count;

        public P(String info, Object lock, AtomicInteger count) {
        //省略
        }

        @SneakyThrows
        @Override
        public void run() {
            synchronized (lock){
                while (this.count.get() < 20){
                   log.info(info + "" +count.incrementAndGet());
                    lock.notifyAll();
//                    lock.wait();
                    lock.wait(1000*20);
                    log.info("100毫秒");
                }    
            }
        }
    }
三个线程交替打印

用ReentrantLock的Condition

public static void main(String[] args){
        AtomicInteger count = new AtomicInteger(0);

        ReentrantLock lock = new ReentrantLock();
        // 声明互相等待的条件
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();
        Condition condition3 = lock.newCondition();
        new Thread(new P2(lock,condition1,condition2,"a",  count),"aaa").start();
        new Thread(new P2(lock,condition2,condition3,"b",  count),"bbb").start();
        new Thread(new P2(lock,condition3,condition1,"c",  count),"ccc").start();
        lock.lock();
        condition1.signal();
        lock.unlock();
    }
    
static class P2 implements Runnable{
        public P2(ReentrantLock lock,Condition condition, Condition conditionNext, String info, AtomicInteger count) {
            this.lock = lock;
            this.condition = condition;
            this.conditionNext = conditionNext;
            this.info = info;
            this.count = count;
        }
        ReentrantLock lock;
        Condition condition;
        Condition conditionNext;
        String info;
        AtomicInteger count;
        @SneakyThrows
        @Override
        public void run() {
                while (this.count.get() < 20) {
                    lock.lock();
                    condition.await();
                    log.info(info + "" + count.incrementAndGet());
                    conditionNext.signal();
                    lock.unlock();
                }    
                
            }
            
        }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324767.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号