core java interview point (2)

Continuing from the previous article (1)

 

11. The difference between HashMap and ConcurrentHashMap, the underlying source code of HashMap.

 HashMap is a collection map in the form of key-value pairs, which is composed of array and linked list data structures. ConcurrentHashMap is a thread-safe HashMap "rewrap", which is a HashMap annotated by segment tags of some columns.

 For specific explanations, please refer to my blog "The difference between HashMap and ConcurrentHashMap, the underlying source code of HashMap"

 

 

12. The difference between TreeMap, HashMap, and LindedHashMap.

The Map collection is the top-level parent interface of the Java collection tool class, and stores data in the form of key-value pairs.

HashMap is the most commonly used implementation class of the Map interface. It stores data according to the hashcode value of the key, and uses get(1) or get('key') to obtain the value. The number of accesses is fast and the traversal order is random. HashMap allows the key of a record to be null and the value to be null. HashMap is not thread-safe. You can use the collection tool class Collections.synchronziedMap() to wrap it in thread-safety, return a thread-safe map or use ConcurrentHashMap. HashTable is similar to HashMap, but it is a subclass of Dictionary class, does not allow key or value to be bull and is thread-safe.

TreeMap is the implementation class of SortMap, which can sort the data it saves according to the key. The default ascending order (ASC) can also override the sorting method, so it is ordered when traversing with Iterator.

LinkedHashMap is a subclass of KashMap, which stores the order of insertion in an orderly manner.

 

 

13. Collection package structure, the difference from Collections.

Collection is the top-level parent interface of the collection tool class. The implementation classes include List, Set, etc. The Collections collection tool class provides public processing methods of the collection class, such as sorting, synchronziedMap, synchronziedList and other methods.

 

14. try catch finally, if there is return in try, is finally still executed?

Try...catch is used to catch exceptions and handle them, and finally is used in this syntax. Finally, it is used to handle logic that needs to be completed regardless of whether there is an exception or not, and is executed regardless of whether there is a return.

 

15. Excption and Error packet structure. What situations have you encountered in OOM and what situations have you encountered in SOF?

Both Ecveption and Error are subinterfaces of the top-level exception interface Throwable. The Throwable interface contains a snapshot of the execution thread execution stack when a thread is created, and provides interfaces such as printStackTrace() for obtaining stack trace data and other information.

Exception: Runtime exception RuntimeException and non-runtime exception

  Runtime exceptions are exceptions that are not checked by the java compiler and will occur when the program is running, such as ArithmaticException, NullpointException, OutOfMemoryException, IndexOutOfBoundException, etc.

Non-runtime exceptions are generally custom exception classes that need to be displayed in the code we provide.

 

Error: Used to indicate that the program is running reasonably due to factors other than the system such as system errors.

 

OOM: OutOfMemoryException abnormal memory overflow, which is an exception thrown due to insufficient memory during the running of the virtual machine jvm memory, and the program stops running

 

16. Three characteristics and meanings of object-oriented in Java.

Abstraction: The splitting of common parts of a topic and the omission of details in order to more fully describe such common features.

Inheritance: Inheritance is a way that subclasses can express commonality by reusing the method properties of the superclass. A new object can derive or modify some object methods from an existing object, and the new object is still free to add new methods to make it more suitable for special needs.

Encapsulation: Rewrite logic such as process and data in a new method, and access to it can only be accessed through this defined interface.

Polymorphism: The reference of the parent class is different because of different subclass implementations under the same behavior. This leads to:

Reference variable types: compile-time and runtime types. The compile-time analogy is determined by the type used when declaring the variable, and the runtime type is determined by the object that is actually assigned to the variable.

 

17. Override和Overload的含义去区别。

Override重写是在对象继承机制中,子类对象对父类或者超类中某一个方法的逻辑进行的特殊改变以适应新需

Overload重载,是在同一个类中,具有相同的名称的方法,但是参数的个数,类型或者位置不同。与方法返回数据类型无关。

 

18. Interface与abstract类的区别。

abstract抽象类,我们知道在java中,所有的对象都是通过类来描述,但是不是所有的类都来描述对象,对于一个没有足够信息来描述一个具体的对象,而需要更加具体的类来支撑和补充,那么这样的类我们称之为抽象类。

Interface接口,是定义的某一个领域的一种规则或者协议,所有实现该接口的类都必须遵循这些协议并实现他们。它是一种形式,是抽象类的延伸和抽象化,一个类可以同时实现多个接口使用关键值inplements标识。

两者都是不可以直接实例化,而需其子类或者实现类来完成

 

19. Static class 与non static class的区别。

static class内部静态类,non static class非静态内部类。

内部静态类不需要有指向外部类的引用,不能够访问外部类的非静态成员,

但非静态内部类需要有对外部类的引用,即不能脱离外部类实例被创建,能够访问外部类的静态和非静态成员。

nested class are devided into two categories: static and non-static. Nested classed that are declared static are called static nested classes, non-static nested classed are called inner class.

内部静态类是称为内部嵌套类,非静态内部类称为内部类。

嵌套:自己完全独立,就是借用壳而隐藏自己

内部类:是一部分,了解你并知道全部,没有外部类就不存在内部类。

 

20. java多态的实现原理。

java多态,就是同一个操作作用于不同的对象,却有不同的方法逻辑和执行结果,父类的引用指向子类的对象实例。它允许基类的引用指向派生类的对象,在具体访问时实现方法的动态绑定。

 

21. 实现多线程的两种方法:Thread与Runable。

线程接口是Runable,java也提供了一个实现类Thread,我们初始化一个线程对象,我们既可以继承线程类Thread也可以实现Runable接口实现run方法。

Thread th = new Thread(new Runable(override run()));

th.start();

使用实现方式,能够避免单继承而导致的某些局限性。

 

22. 线程同步的方法:sychronized、lock、reentrantLock等。

何为同步?

我们使用一个变量,这个变量在第一个线程中进行读写操作,第二个线程同时访问该变量,可能会导致第一个线程读取的该变量的值是第二个线程改变的,反之亦然。这样一个变量在并发情况下值不能保证“一致性”的情况,我们称之为非线程安全。

synchronzied关键字,是java提供的最基础的用于线程安全锁。用于修饰类,方法,代码块。在修饰代码块时候,必须绑定一个reference对象作为锁对象,我们可以使用this当前对象。在修饰方法时候默认就是当前对象。修饰类时候默认当前类的class对象作为锁对象。

synchronzied的锁机制是使用monitorenter和monitorexit字节码指令,在每次进入代码块时候执行monitorenter获取对象锁,如果对象没有被锁或者已经是当前对象锁定,那么锁计数器加一,退出同步块monitorexit减1、直到0主动释放该对象的锁。因此synchronzied是可重入得,不会出现自己把自己锁死的。

在Java1.5以后,增加了reentrantLock和Atomic:

ReentrantLock,以对象的方式操作对象锁,相对于synchronzied需要手动finally释放锁,并且多了其他功能:

1.等待可中断:在持有锁的线程长时间无法释放锁时候,等待的线程可以选择主动放弃,tryLock(long timeout, TimeoUnit unit)

2.公平锁:所有的线程是按照申请的顺序来获取锁称之为公平锁。synchronzied是非公平的,线程的优先级越高,获取锁越高。new ReentrantLock(boolean fair)

3.绑定多个condition:通过多次new Condition可以获取多个condition对象,可以简单实现比较复杂的线程同步,通过awati(), signal()

 

相对于ReentrantLock,synchronzied是直接在JVM层面上实现的,不但可以通过监控工具监控synchronzied锁定,而且在程序出现异常JVM会自动释放锁对象,但是Lock不行,它是通过代码实现的,必须保证锁释放,unLock()放到finally{}中。

在资源竞争不激烈的情况下,synchronzied性能优于ReentrantLock,但是竞争激烈时,synchronzied性能会下降,但是ReentrantLock不变化。

 

23. 锁的等级:方法锁、对象锁、类锁。

方法锁和对象锁是一样的。只有方法锁和类锁两种。都是在java中synchronzied关键字时候,涉及到锁的问题。

java内置锁,每个java对象都可以用作一个实现同步的锁,这些所称之为内置锁。线程进入同步代码块或者方法时候会自动获得锁对象,在退出同步代码块时候释放。触发内置锁,就是程序调用了synchronzied修饰的方法或者代码块。该内置锁是一个互斥锁,一个线程获得锁后,其他线程只能在该线程释放锁之前等待或者阻塞,直到释放。

java的对象锁和类锁:对象锁使用与对象的实例方法,或者一个对象实例,类锁则是用于类的静态成员变量或者类的class上。类的实例对象可以有很多歌,但是每个类只有一个class,所以不同对象锁是互不干扰,但是类锁只有一个。类锁是一个概念,并不真实存在,我们在使用类的静态成员变量时候永远获取的只有一个,就是可以理解为该类锁。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326359793&siteId=291194637