美文网首页
2021 JAVA面试题精选

2021 JAVA面试题精选

作者: 木木子丶 | 来源:发表于2021-02-18 17:13 被阅读0次

手动整理不易,持续更新

详细描述ThreadPoolExcutor各个参数的含义,介绍一个任务提交到线程池后的执行流程

int corePoolSize
核心线程数 表示线程池的常驻核心数.刚提交任务的时候,每来一个任务就会创建一个线程,直到达到核心线程数.

int maximumPoolSize
最大线程数 用于表示线程池能维护最大的线程数量.

long keepAliveTime 
过期时间 非核心线程的过期时间

TimeUnit
过期单位 非核心线程的过期单位

BlockingQueue<Runnable> workQueue 
任务队列 用来存放未执行的任务

ThreadFactory threadFactory 
线程工厂 用来方便标记所使用的线程

RejectedExecutionHandler handler 线程池关闭或任务过多来不及处理时的拒绝策略
4种策略 :报错、让启动线程池的线程执行、丢掉、丢掉最早的任务

简要说下Servlet的生命周期

1. 创建servlet实例
2. 当servlet实例化后,调用这个对象的init()方法实例化
3. 再调用对象的service()方法来处理请求,并返回处理结果
4. 当需要释放servlet对象时,调用distroy()方法来结束,释放资源

(init和distroy方法只执行一次)

请编码实现单例模式

public class Singleton {
    private static volatile Singleton sing;

    public static Singleton getSing() {
        if (null == sing) {
            synchronized (Singleton.class) {
                if (sing == null) {
                    sing = new Singleton();
                }
            }
        }
        return sing;
    }
}

写一个Map转换成JavaBean的工具类方法,实现如下mapToObject方法(使用Java反射,不允许使用第三方类库)

    public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) throws Exception {
        T object = beanClass.newInstance();
        for (String field : map.keySet()) {
            Field classField = beanClass.getDeclaredField(field);
            classField.setAccessible(true);
            if (Modifier.isStatic(classField.getModifiers()) || Modifier.isFinal(classField.getModifiers())) {
                continue;
            }
            if (null != classField) {
                classField.set(object, map.get(field));
            }
        }
        return object;
    }

介绍HashMap的数据结构、扩容机制,HashMap与Hashtable的区别,是否是线程安全的,并介绍ConcurrentHashMap的实现机制

HashMap 是一个数组链表加红黑树形式的数据结构,当链表的长度大于8将会转为红黑树的结构存储

HashMap默认初始容量为16,加载因子为0.75,当容量大于16*0.75时候扩容两倍,将原本数据克隆岛新数组上

hashMap是线程不安全的 性能高 迭代器遍历索引从小到大 初始容量为16,扩容为size*2 ,hashtable是线程安全的 性能低 迭代器遍历索引从大到小 初始容量为11,扩容为size*2 +1

ConcurrentHashMap是分段锁的形式, 如果线程A来查询可能分配a段形式查询,线程B来查询分配b段查询,并且两端查询都为线程安全

什么是死锁?JAVA程序中什么情况下回出现死锁?如何避免出现死锁?

死锁就是两个线程各自等待对方的锁释放,造成的一种僵局

根据造成死锁的条件去反向推就可以
1. 互斥条件: 一个资源每次只能被一个线程使用
2. 请求与保持条件: 一个线程因请求资源而阻塞时,对已获得的资源保持不放
3. 不剥夺条件: 线程已获得资源,在未使用完之前,不能强行剥夺
4. 循环等待条件: 若干线程之间形成一种头尾相接的循环等待资源关系 


请大致描述一下BIO,AIO和NIO的区别?

BIO 同步阻塞式IO,服务器实现模式为1个连接一个线程,即客户端有连接请求时,服务器端就会启动一个线程进行处理,如果这个线程不做任何事情会造成不必要的线程开销,当然可以通过线程池机制改善

NIO 同步非阻塞式IO,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有I/O请求时才启动一个线程进行处理.

AIO 异步非阻塞式IO,服务器实现模式为一个有效请求一个线程,客户端的IO请求都是由OS先完成了,再去通知服务器去启动线程进行处理

建立三个线程A、B、C,A线程打印10次字母A,B线程打印10次字母B,C线程打印10次字母C,但是要求三个线程同时运行,并且实现交替打印,即按照ABCABCABC的顺序打印。

   我知道有人第一眼就想到什么lock,conditional什么玩意的,但是有更简单的写法.

    @Test
    public void test() {

        for (int x = 1; x <= 10; x++) {
            CompletableFuture.runAsync(() -> {
                System.out.println("线程A");
            }).thenRunAsync(() -> {
                System.out.println("线程B");
            }).thenRunAsync(() -> {
                System.out.println("线程C");
            });
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


手写个单链表数据结构

package com.zimu.yuque;

import com.fasterxml.jackson.databind.util.LinkedNode;
import org.w3c.dom.Node;

/**
 * @PROJECT_NAME: 杭州品茗信息技术有限公司
 * @DESCRIPTION:
 * @author: 徐子木
 * @DATE: 2021/2/19 1:39 下午
 */
public class linkedNode<T> {

    @Override
    public String toString() {
        return "linkedNode{" +
                "head=" + head +
                '}';
    }

    //头结点
    private Node head;


    /**
     * 单链表结点类
     *
     * @param <T>
     */
    private class Node<T> {
        //数据域
        T value;
        //指针域
        Node next;

        public Node(T value) {
            this.value = value;
            next = null;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    ", next=" + next +
                    '}';
        }
    }


    /**
     * @param : 头部插入结点
     * @desc : 如果链表没有头结点,新节点直接成为头结点;反之新节点的next直接指向头结点,并让新节点成为新的头结点
     * @dete : 2021/2/19 1:48 下午
     * @Return:
     * @author: 徐子木
     */
    public void addHead(T value) {
        Node newNode = new Node(value);
        //头结点不存在,新节点成为头结点
        if (head == null) {
            head = newNode;
            return;
        }
        newNode.next = head;
        head = newNode;
    }

    /**
     * @param :
     * @desc : 如果链表没有头结点,新节点直接成为头肩点;反之需要找到链表当前的尾结点,并将新节点插入链表尾部
     * @dete : 2021/2/19 1:55 下午
     * @Return:
     * @author: 徐子木
     */
    public void addTail(T value) {
        Node newNode = new Node(value);
        if (head == null) {
            head = newNode;
            return;
        }

        Node last = head;
        while (last.next != null) {
            last = last.next;
        }
        last.next = newNode;
    }

    /**
     * @param :
     * @desc : 添加指定节点
     * @dete : 2021/2/19 5:12 下午
     * @Return:
     * @author: 徐子木
     */
    public void addAtIndex(T value, int index) {
        if (index < 0 || index > size()) {
            throw new IndexOutOfBoundsException("索引异常");
        }

        if (index == 0) {
            addHead(value);
        } else if (index == size()) {
            addTail(value);
        } else {
            Node newNode = new Node(value);
            int position = 0;
            Node cur = head; //当前节点
            Node pre = null; //记录前置节点

            while (cur != null) {
                if (position == index) {
                    newNode.next = cur;
                    pre.next = newNode;
                    return;
                }
                pre = cur;
                cur = cur.next;
                position++;
            }
        }
    }

    /**
     * 删除指定位置
     *
     * @param index
     */
    public void deleteAtIndex(int index) {
        if (index < 0 || index > size()) {
            throw new IndexOutOfBoundsException("索引异常");
        }

        if (index == 0) {
            head = head.next;
            return;
        }
        //记录当前位置
        int position = 0;
        //标记当前节点
        Node cur = head;
        // 记录前置节点
        Node pre = null;

        while (cur != null) {
            if (position == index) {
                pre.next = cur.next;
                cur.next = null;
                return;
            }
            pre = cur;
            cur = cur.next;
            position++;
        }
    }

    /**
     * 反转
     */
    public void reverse() {
        //标记当前
        Node cur = head;
        //当前前一个节点
        Node pre = null;
        Node temp;

        while (cur != null) {
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        head = pre;
    }

    public int size() {
        int size = 0;
        if (head == null) {
            return size;
        }
        if (head.next != null) {
            Node last = head.next;
            size = 2;
            while (last.next != null) {
                last = last.next;
                size++;
            }
        } else {
            size = 1;
        }
        return size;
    }

    public static void main(String[] args) {
        linkedNode<Integer> list = new linkedNode<>();
        list.addHead(1);
        list.addAtIndex(123, 1);
        list.addTail(456);
        System.out.println("toString:" + list);

        list.reverse();

        System.out.println("reverse:" + list);

        list.deleteAtIndex(2);

        System.out.println("delete:" + list);
    }

}

相关文章

网友评论

      本文标题:2021 JAVA面试题精选

      本文链接:https://www.haomeiwen.com/subject/molnxltx.html