java Miscellany

1.stream Map () FlatMap () difference

map: For Stream element included using a given conversion function to convert the operation, the newly generated element conversion Stream contains only generated.

flatMap: map and the like, which is different for each element is converted Stream object, the child will Stream into the parent element in the collection.

Below is a more intuitive FIG.

 Run about, pay attention to the red mark:

2.wait () notify () notifyAll () syntax scene

wait (), notify (), notifyAll () must be executed within / synchronization method synchronized block, because only the calling thread in obtaining the lock object before calling wait object (), notify (), notifyAll () method, If you call in the absence of the object to obtain the object lock these three methods, IllegalMonitorStateException will throw an exception.

3.Redis Aof Rdb persistent differences

RDB persistence time snapshots generated data set within a specified time interval.

Pros: fast save a set point in time data for backup and restore data sets speed.

Disadvantages: large volumes of data, save the slow, slow if the snapshot frequency, downtime, you may lose large amounts of data.

AOF persistent record of all write operations performed by the server, restore the dataset by re-execute the command.

Advantages: AOF make redis very durable, can set different fsync strategies, AOF is a file only append operation log files, downtime, lost a small amount of data.

Disadvantages: the same set of data, the volume may be larger than the file AOF RDB file size, depending on the strategy fsync, AOF speed may be slower than the speed of RDB.

4. Normal unique index performance index

  • Query process: select id from table where cal = 6;

Regardless of the index, are beginning to start the B + tree roots, layer by searching find leaf node, first find the data page, and then distributed according to a two record positioning.

For the general index, find the records meet the conditions, will continue to look for the following records, know where to find the first record so far does not meet the criteria.

For a unique index, because the index entry defines the uniqueness, so finding will stop retrieving the records meet the conditions.

在查询过程中,普通索引相对与唯一索引仅多做了查找和判断下一条操作,性能差别不大,除非通过普通索引查到的第一条符合条件的记录在数据页的最后一条,这种情况会复杂一些,但是概率很低。

  • 更新过程:

首先要理解一下什么是change buffer:当需要更新一个数据页的某条记录时,如果这个数据页在内存中就直接更新,如果不在内存中,在不影响数据一致性的前提下,InooDB就会将更新操作缓存到change buffer中,这样就无需从磁盘中读取这个数据页,当下次有查询操作访问到这个数据页是,InooDB将数据页读入内存,并且执行change buffer中有关这个数据页的操作。将change buffer记录的操作应用到原数据页的操作叫merge,除访问数据页会触发merge外,后台也有线程会定期merge。使用change buffer 主要是减少IO操作,并且提高内存利用率。对与写多读少对业务,使用change buffer效果好。

对于唯一索引来说,所有的更新操作都要判断操作是否违背唯一性约束,也就是在插入记录之前,会先检索表中是否已经存在符合条件都记录,所以必须先将数据页读到内存,所以使用唯一索引更新操作时,不能使用change buffer。只有唯一索引更新操作时,才可以使用change buffer。

总而言之,更新操作中,普通索引性能较高。

参考:https://www.cnblogs.com/jimmyhe/p/11027141.html

5.stream peek()和map()的区别

peek()的入参为Consumer,Consumer的实现类仅有一个方法,并且返回类型为void。

map()的入参为Function,Function实现类的方法有返回值。

总结:peek接收一个没有返回值的λ表达式,可以做一些输出,外部处理等。map接收一个有返回值的λ表达式,之后Stream的泛型类型将转换为map参数λ表达式返回的类型。

6.单例模式

  • 双锁模式
public class Singleton {
    
    private Singleton(){}
    
    private static volatile Singleton instance;
    
    public static Singleton getInstance(){
        if(instance == null){
            synchronized(Singleton.class){
                if(instance == null){
                    instance = new Singleton();        
                }
            }
        }
        return instance;
    }
}
  • 静态内部类
public class Singleton {
    
    private Singleton(){}
    
    private static class SingletonHolder {
        private static Singleton instance = new Singleton();
    }
    
    public static Singleton getInstance(){
        return SingletonHolder.instance;
    }
}
  • 枚举
public enum Singleton {
    
    INSTANCE;

    public void fun(){}
    
}

7.快速失败机制

集合类中的一种错误检测机制,在迭代集合的过程中,改变的集合的结构,就有可能抛出ConcurrentModificationException,一般用于检测bug,不可依赖。

8.jvm内存模型

  • 程序计数器 作为当前线程的行号指示器
  • java虚拟机栈 存储局部变量和部分运行结果
  • 本地方法栈 native
  • 堆 所有对象实例以及 数组 线程共享
  • 方法区 存储类信息 常量 静态变量等

9.HashMap扩容为何是2的N次方

HashMap无论存取操作,都需要计算数据的hash值mod桶的长度,源码中通过hash&(n-1)位运算计算mod值,n为桶的长度,如果n为2的幂次方,那么n-1转换为二进制后,每一位都是1,与不同key的哈希值取做&运算,将得到不同的下标,减少碰撞的几率,提高存取的效率。

10.volatile 关键字作用

volatile关键词的作用:
  • 每次针对该变量的操作都激发一次load and save。处理数据时,线程会把值load到本地,处理之后再save回去。使用volatile关键字的变量,保证其在多线程之间的可见性,每次读到volatile关键字修饰的变量,一定是最新的数据。
  • 禁止重排序

11.Lambda List 转Map 如何键值重复处理

 12.线程池创建核心参数以及拒绝策略

  • 核心参数
corePoolSize: 线程池核心线程数最大值
maximumPoolSize: 线程池最大线程数大小
keepAliveTime: 线程池中非核心线程空闲的存活时间大小
unit: 线程空闲存活时间单位
workQueue: 存放任务的阻塞队列
threadFactory: 用于设置创建线程的工厂,可以给创建的线程设置有意义的名字,可方便排查问题。
handler: 线城池的饱和策略事件,主要有四种类型。
  • 四种拒绝策略
AbortPolicy(抛出一个异常,默认的)
DiscardPolicy(直接丢弃任务)
DiscardOldestPolicy(丢弃队列里最老的任务,将当前这个任务继续提交给线程池)
CallerRunsPolicy(交给线程池调用所在的线程进行处理)

Guess you like

Origin www.cnblogs.com/youtang/p/11920896.html