Java interview question 2

cd card 2022-02-13 07:59:30 阅读数:573

java interview question

1、Redis What to do if the cache space is full ?

        answer :Redis There is a corresponding elimination mechanism in , Used to deal with insufficient cache space , We just need to make reasonable configuration .

Generally, we are caching data to redis You can set the expiration time of the cache , Lest the data in the cache keep growing . But we still have to consider special circumstances , When the cache is full , But the expiration time we set hasn't come yet , At this time, if the cache space is not processed , Memory overflow will occur .redis Specialized secondary schools have an elimination mechanism for this situation .

        1、lru Eliminate the earliest accessed data -- In the form of a linked list , The earliest accessed data is placed in the chain header , The newly accessed data is placed at the end of the linked list , When there is not enough memory , First delete the data in the head of the linked list .

        2、lfu Eliminate the data with the lowest access rate -- How to ensure that access is not affected by new and old data ?--redis A timestamp and counter are set for each new data , Only when the counter reaches a certain value , Or after a certain time , This new data will be compared with other old data . And the of the counter is over time , If there is no access, the counter will gradually decay .

        3、 To be eliminated at random -- Randomly select a part of the data and delete it , Generally do not use

2、servlet Life cycle of ?

        answer : initialization : load servlet class , And call init()

                Use stage : call service() Method ,

                 Closing phase : call destory() Method , here servlet Will wait for jvm The garbage collection of

3、 Class in jvm Life cycle in ?

        answer : load :jvm Load bytecode file

                 Connect : There are three steps

                         verification : Check whether the class loaded by bytecode file conforms to jvm Specification of classes .

                         Get ready : Is a static variable , The static method divides the static area in the method area , And assign the initial value to the static variable , The integer is 0, The character type is null, Boolean is false

                         analysis : Convert symbolic references to direct references , Is to assign the address value to the corresponding reference object

        initialization : Initialize and assign the variables in the class , The assignment at this time is to assign the quantity defined in the class to the variable

                Use : Define the object , To use

                 uninstall : After the class has no reference jvm Garbage collection

4、 How to optimize search ?

        answer : By default, the search at this time obtains the data results through traversal

                1、 You can refer to elasticSearch Operations on data in , Create a class , The inverted index method is used to store all data in it , When searching, use this class to query .

                2、 You can also make the data in the form of binary tree , When querying , It will also be faster to find data in the double binary tree

5、hashmap and hashtable The difference between ?

        answer :1、hashMap It's not thread safe ,hashtable It's thread safe

                2、hashmap in key,value It can be for null,hashtable Not in the middle

                3、hashmap and hashtable The inherited parent class is different , And hashtable Inherited parent classes and are obsolete

6、 The life cycle of a thread ?

        answer : Freshmen -- be ready -- function --( Blocking )-- Death

7、 What are the status of threads , And how to achieve this state ?

        answer : be ready : call start() When the method is used , The thread enters the thread pool and waits cpu Call to

                function :cpu Allocate time slices to corresponding threads

                Blocking : Blocking can also be called waiting state

                                Finite waiting state :sleep(timeout)  wait(timeout) join(timeout)

                                  Waiting indefinitely :wait()  join()

                                        wait() If you don't define time , Will always be in a waiting state , Until another thread passes notify()/notifyAll() Method to wake it up .

                                        join() If the expiration time is not set , Will always carry out wait(0) Methods , Until the thread dies .

                Death : When the thread ends, it will die automatically

8、wait() and sleep() Differences in methods :

        answer :wait() yes Object Methods .sleep() yes Thread Class method

         call wait() When the method is used , The thread will release cpu resources , Until it is notify Method wake up ,sleep() Method does not release resources when called , Wait until the end of sleep time to continue

9、 Features of thread pool ?

        answer : We usually use ThreadPoolExcuter To create a thread pool

                When threads are not used much , The core thread is used by default , The number of tasks has increased , Not enough core threads , It will gradually expand to the maximum number of threads , If the number of tasks is still increasing , The thread is fully loaded , The waiting line is full . The reject policy is triggered , Just don't continue to receive tasks . When the number of tasks decreases , In addition to the core thread , The rest of the threads will wait for a while , Post death .

10、 How to deal with the result set returned by the database ?

        answer : The explanation here is springboot,mybatisplus The situation of

                First, create a class with the same type as that in the access result and a corresponding dao class , Then make the class accessing the database inherit mybatisplus Medium ServiceImpl class , And will ServiceImpl The genericity of is set to <dao class , Class name >.ServiceImpl Through id Or get the result set through the filter , The resulting set is a collection of previously established classes .

11、spring What are the core modules of ?

        answer :iop Container module

                aop modular

                Database module

                web Refer to the module

                Test module

12、Redis Cache penetration , Cache breakdown , What does cache avalanche mean and how to solve it

        answer : Cache penetration : Access data that does not exist in the database , Under normal circumstances, if there is no data in the query database , return null No, put the results in the cache , This causes each query to access the database once .

        terms of settlement :1、 Using the bloon filter   2、 Cache returned null data

                Cache breakdown : Access a piece of data existing in the database , And the concurrency of this access is very large , Cache breakdown occurs in redis Of this data in key When it expires , A large number of concurrent accesses to the database at the same time , Cause the server to crash .

        terms of settlement : Use mutex , When value When the value is empty, it is not for us to access the database , But through redis Self contained setnex  (set if not exit) To load the corresponding key,value. When the return result is true when , Indicates that the corresponding data in the database has been cached to redis Yes , Large concurrent data is accessed at this time redis The data in

                Cache avalanche : Different data access , There's a lot of concurrency , If redis in key Expire at the same time , It will also cause a large amount of data to directly access the database ,

        terms of settlement :1、 Decentralized cache expiration time 2、 You can set the hotspot data to never expire

Explain it in advance , These are my interview questions. They are all the answers I found online , Combined with my own words to understand , Not necessarily right , Mainly as my personal summary .

copyright:author[cd card],Please bring the original link to reprint, thank you.