java常规技术题(Java general technical questions)

一:线程的基本状态以及状态之间的关系
1.新建状态:新创建了一个线程对象
2.就绪状态:也叫可运行状态,线程对象创建后,其他线程调用了该对象的start()方法。
3.运行状态:就绪状态的线程获取了cpu,执行程序代码
4.阻塞状态:阻塞状态是线程因为某种原因放弃CPU执行权,暂时停止运行,直到线程进入就绪转态,才有机会转到运行转态,阻塞的情况分三种:等待阻塞,同步阻塞,其他阻塞

二:描述java锁机制
java中所说的锁就是内置锁,每个java对象都可以作为一个实现同步的锁,虽然说在java中一切皆对象,但是锁必须是引用类型的,基本数据类型则不可以。

三:启动一个线程是用run()还是start()?
启动一个线程是调用start()方法,使线程就绪转态,以后可以被调度为运行转态,一个线程必须关联一些具体的执行代码,run()方法是该线程所关联的执行代码。

四:set和list的区别,list和map的区别?
1.set是无序的,元素不可重复;list是有序的,元素可以重复
2.list存储的是单个对象的集合(有序的),map存储的是键值对为对象的集合(无序的);

五:同步方法,同步代码块区别?
1.同步方法:即有synchronized关键字修饰的方法。在使用该方法前,需要获得内置锁,否则就处于阻塞状态。
2.同步代码块:即有synchronized关键字修饰的语句块,被该关键字修饰的语句块会自动被加上内置锁,从而实现同步。

六:comparable和comparator接口是干什么的,说出他们的区别
它们都是用于对类的对象进行排序和比较使用的接口
1.comparable是排序接口,位于java.lang包下,且重写了compareTo方法,就意味着该类支持排序,常结合Collections.sort或Arrays.sort对集合或数组内的元素进行排序。
2.comparator是比较接口,位于java.util包下,我们如果需要控制某个类对象的次序,而该类本身不支持排序(即没有实现Comparable接口),那么我们就可以建立一个“该类的比较器”来进行排序,这个“比较器”只需要实现Comparator接口即可。
3.comparable相当于“内部比较器”,而comparator相当于“外部比较器”

七:java集合类框架的最佳实践有哪些?
首先Java中的集合框架体系非常强大和完善,主要用于程序中的数据存储,从最顶层主要分为了Collection和Map接口,我们平时使用的集合类都是从这两个类别中扩展开来,正确选择要使用的集合的类型对性能非常重要。
比如:
1,元素的大小是固定的,而且能事先知道,我们就应该用Array而不是ArrayList。
2)如果我们大概知道存储的数量,可以在使用集合时先给予一个初始容量大小,从而有效避免集合自动增长的算法而造成的空间浪费,如new ArrayList(30)。
3,为了类型安全,提高存取效率和可读性,我们优先使用泛型,并且还能有效避免ClassCastException类型转换异常。
4,有时为了提高数据的快速定位查找,可优先使用Map键值对集合,因为Map集合在数据的查找上效率非常高,但是如果要保证数据的顺序,最好使用List
5,使用JDK提供的不变类作为Map的键可以避免为我们自己的类实现hashCode()和equals()方法。
6,底层的集合实际上是空的情况下,返回长度是0的集合或者是数组,不要返回null。

八:hashmap和hashtable的区别?
1.hashmap和hashtable都说键值对数据结构,且都实现了map接口,存储的元素无序。
2.hashmap非线程安全的,hashtable是线程安全的,所以hashmap的效率高于hashtable(HashTable里面的方法使用Synchronize关键字修饰)
3.hashmap允许空键空值,hashtable则不允许。

九:hashset和treeset有什么区别?
相同点:1.单列存储,2.元素不可重复
不同点:1.底层数据结构不同(hashset哈希表结构,treeset二叉树结构)
2.数据唯一性依据不同(HashSet通过重写hashcode和equals TreeSet通过compareable接口)
3.有序性不同,hashset无序,treeset有序

十:说出ArrayList,vector,linkedlist的存储性能和特性。
1.ArrayList,vector,linkedlist都实现了list接口;
2.ArrayList和vector底层是用数组实现的,而linkedlist使用双向链表实现
3.在容量增长上,ArrayList增长原来50%,vector集合增加容量为原来的一倍.
4.安全性方面vector能保证线程安全,但是效率比ArrayList要低

————————

1: The basic state of the thread and the relationship between the States
1. New status: a new thread object is created
2. Ready state: also called runnable state. After a thread object is created, other threads call the start () method of the object.
3. Running status: the thread in ready status obtains the CPU and executes the program code
4. Blocking state: the blocking state is that the thread gives up the CPU execution right for some reason and temporarily stops running until the thread enters the ready state. There are three kinds of blocking conditions: waiting blocking, synchronous blocking and other blocking

2: Describe Java lock mechanism
The lock mentioned in Java is a built-in lock. Each Java object can be used as a lock to realize synchronization. Although everything is an object in Java, the lock must be of reference type, but the basic data type cannot.

3: Do you start a thread with run() or start()?
To start a thread is to call the start () method to make the thread ready for state transition. Later, it can be scheduled to run state transition. A thread must be associated with some specific execution code, and the run () method is the execution code associated with the thread.

4: What is the difference between set and list, and between list and map?
1. Set is out of order and elements cannot be repeated; List is ordered, and elements can be repeated
2. List stores a set of single objects (ordered), and map stores a set of key value pairs (unordered);

5: What is the difference between synchronization method and synchronization code block?
1. Synchronization method: that is, the method decorated with synchronized keyword. Before using this method, you need to obtain the built-in lock, otherwise it will be blocked.
2. Synchronous code block: that is, the statement block modified by the synchronized keyword. The statement block modified by the keyword will be automatically added with a built-in lock to realize synchronization.

6: What do the comparable and comparator interfaces do? Tell the difference between them
They are all interfaces used to sort and compare objects of classes
1. Comparable is the sorting interface, which is located in Java Lang package, and the CompareTo method is rewritten, which means that this class supports sorting, often combined with collections Sort or arrays Sort sorts the elements in a collection or array.
2. Comparator is a comparison interface, located in Java Under the util package, if we need to control the order of a class object, and the class itself does not support sorting (that is, it does not implement the comparable interface), then we can establish a “comparator of this class” to sort. This “comparator” only needs to implement the comparator interface.
3. Comparable is equivalent to “internal comparator”, while comparator is equivalent to “external comparator”

7: What are the best practices of the Java collection class framework?
First of all, the collection framework system in Java is very powerful and perfect. It is mainly used for data storage in programs. From the top level, it is mainly divided into collection and map interfaces. The collection classes we usually use are extended from these two categories. Correctly selecting the type of collection to be used is very important for performance.
For example:
1. The size of elements is fixed and can be known in advance. We should use array instead of ArrayList.
2) If we know the amount of storage roughly, we can give an initial capacity when using the set, so as to effectively avoid the waste of space caused by the algorithm of automatic growth of the set, such as new ArrayList (30).
3. In order to ensure type safety and improve access efficiency and readability, we give priority to generic types, and can effectively avoid ClassCastException type conversion exceptions.
4. Sometimes, in order to improve the fast positioning and search of data, the map key value pair set can be used first, because the map set is very efficient in data search, but if you want to ensure the order of data, you’d better use list
5. Using the invariant class provided by JDK as the key of map can avoid implementing hashcode () and equals () methods for our own classes.
6. When the underlying set is actually empty, return the set or array with length 0. Do not return null.

8: What is the difference between HashMap and hashtable?
1. Both HashMap and hashtable refer to the key value pair data structure, and both implement the map interface, and the stored elements are out of order.
2. HashMap is non thread safe and hashtable is thread safe, so the efficiency of HashMap is higher than hashtable (the methods in hashtable are modified with the synchronize keyword)
3. HashMap allows null keys and null values, while hashtable does not.

9: What’s the difference between HashSet and TreeSet?
Similarities: 1 Single column storage, 2 Element cannot be repeated
Differences: 1 Different underlying data structures (HashSet hash table structure, tree binary tree structure)
2. Data uniqueness is based on different (HashSet rewrites hashcode and equals TreeSet through comparable interface)
3. Different order, HashSet disorder and TreeSet order

10: Name the storage performance and characteristics of ArrayList, vector and LinkedList.
1. ArrayList, vector and LinkedList all implement the list interface;
2. The bottom layers of ArrayList and vector are implemented by array, while LinkedList is implemented by two-way linked list
3. In terms of capacity growth, ArrayList increased by 50%, and the capacity of vector set doubled
4. In terms of security, vector can ensure thread safety, but its efficiency is lower than ArrayList