当前位置:网站首页>Android interview theme collection

Android interview theme collection

2022-04-23 13:49:00 InfoQ

2) The thread executes a piece of synchronization code , But can't get the related synchronization lock , It can only enter the blocking state , etc.   Get synchronization lock , To resume execution ;

3) The thread executed the... Of an object  wait() Method , Directly into the blocking state , Waiting for other threads to execute  notify()/notifyAll() operation ;

4) Threads perform some  IO  operation , Due to waiting for related resources, it enters the blocking state , Such as  System.in,  But no input from the keyboard , Then enter the blocking state .

5) Thread comity ,Thread.yield() Method , Pause the currently executing thread object , Take execution opportunities   Give up to threads of the same or higher priority , But it doesn't put the thread into blocking state , The thread is still executable   state , At any time, you may get another share of  CPU  Time . Thread self closing ,join() Method , Call the... Of another thread in the current thread  join() Method , The current thread is in blocking state , Until the end of another thread , At present   The thread changes from blocking to ready .

6) Threads execute  suspend() Put the thread into blocking state , must  resume() Method is called , Can we make   Thread re enters executable state .

[](()7? How does the thread close ?

1) Use flag bits

2) Use  stop() Method , But it's like turning off the computer , Something unexpected may happen   problem

3) Use interrupt  interrupt()

public class Thread {

//  Interrupt current thread

public void interrupt();

//  Determine whether the current thread is interrupted

public boolen isInterrupt();

//  Clear the interrupt status of the current thread , And return the previous value

public static boolen interrupted();

}

But the call  interrupt() Method just passes the interrupt request message , Doesn't mean to stop the target thread immediately .

[](()8、 Let's talk about  java  The method of synchronization in

Why synchronization is needed , Because in multithreading concurrency control , When multiple threads operate on a shareable resource at the same time   Source time , If there is no synchronization mechanism , It will lead to inaccurate data , So we need to add synchronization lock , indeed   Ensure that the thread is called by other threads before the operation is completed , So as to ensure the uniqueness and accuracy of the variable .

1)synchronized  Pertaining to a synchronous code block or method

because  java  Each object of has a built-in lock , When using this keyword to decorate a method , Built in lock will protect   The whole method . Before calling this method , Need to get a built-in lock , Otherwise, it will be in a stuffy state .

2)volatile  Modifying variables

Ensure the visibility of variables between processes , Every time a thread accesses  volatile  Modified variables are from memory   Read , Instead of caching , In this way, the variables accessed by each thread are the same . And use a memory barrier .

3)ReentrantLock  Reentrant lock , Its common methods are  ReentrantLock():

Create a  ReentrantLock  example  lock() Gets the lock  unlock() Release the lock

4) Use local variables  ThreadLocal  Thread synchronization , Each thread will save a copy of the variable   copy , The copies are independent of each other , In this way, each thread can modify its own copy at will , Without affecting other threads . Common methods  ThreadLocal() Create a thread local variable ;get() Returns this thread   The current thread copy variable of the part ;initialValue() Returns the initial value of the current thread for this thread local variable   value ;set(T value) Set the value in the current thread copy of this thread variable to  value

5) Using atomic variables , Such as  AtomicInteger, Common methods  AtomicInteger(int value) gen   Build a with a given initial value  AtomicInteger  Integers ;addAndGet(int data) In an atomic way   Add the given value to the current value

6) Thread synchronization using blocking queue  LinkedBlockingQueue

[](()9、 How to ensure thread safety ?

Thread security is embodied in three methods :

1) Atomicity : Provide exclusive access , Only one line and data can be operated at the same time .

JDK A lot of atomic class , Such as  AtomicInteger\AtomicBoolean\AtomicLong, They are through  CAS  Complete atomicity . JDK  There are two types of locks available :synchronized  rely on  JVM  Implementation lock , The of the keyword object   Within the scope, only one thread can operate at the same time . The other is  LOCK, yes  JDK  Code level locks provided , rely on  CPU  Instructions , The representative is  ReentrantLock.

2) visibility : One thread's modification to main memory is seen by other threads in time .

JVM  Provides  synchronized  and  volatile,volatile  Visibility is through memory barriers and prohibitions   The implementation of weight stop sorting ,volatile  When writing operations , Add a... After the write operation  store  Barrier directive ,  Refresh shared variable values in local memory to main memory ; When reading operations , 
《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》 Free open source   Hui Xin search official account 【 Advanced programming 】
  Add a... Before reading  load  Instructions , Read shared variables from memory .

3) Orderliness : Instructions are not reordered by the compiler .

It can be done by  volatile、synchronized、Lock  To ensure order .

[](()10、 Both processes require writing or reading at the same time , Can we achieve ? How to prevent process synchronization ?

I think it can be achieved , For example, there is no problem for both processes to read calendar process data , But at the same time write , Should be   There will be conflict .

Shared memory can be used to share data between processes .

null
[](() Two 、Android  Interview help


[](()1、 Make it as smooth as possible  ListView, How do you optimize your work ?

①Item  Layout , The fewer levels, the better , Use  hierarchyview  Tool view optimization .



② Reuse  convertView



③ Use  ViewHolder



④item  When there are pictures in , Load asynchronously



⑤ When sliding fast , Don't load pictures



⑥item  When there are pictures in , The picture should be compressed properly



⑦ Realize the paging loading of data

[](()2、 about  Android  Security issues , How much do you know

① Error exporting component



②  Parameter verification is not strict



③WebView  Introduce various security issues ,webview  Medium  js  Inject



④ No confusion 、 No second packing



⑤ Clear text stores key information



⑦  Misuse  HTTPS



⑧ Shanzhai encryption method



⑨ Abuse of authority 、 Memory leak 、 Use  debug  Signature

[](()3、 How to reduce it  APK  Bag size ?

Code to maintain good programming habits , Don't duplicate or unused code , Add carefully  libs, Remove unused  libs.

Use  proguard  Obfuscated code , It optimizes unused code , And confusion can also reduce safety   The size of the package .

native code  Part of , Most of the time, just support  armabi  And  x86  The architecture of . If   Not necessary , Think about taking it off  x86  Part of .

resources

Use  Lint  Tools look for unused resources . Remove unused images ,String,XML  wait . assets  Please make sure there are no useless files in the directory .

Generate  APK  When ,aapt  The tool itself will be right  png  Make optimization , But before that, you can also use its   His tools are like  tinypng  The image is further compressed and preprocessed .

jpeg  still  png, Choose according to your needs , At some point  jpeg  Can reduce the size of the picture .  about  9.png  Pictures of the , Cut the stretchable area as small as possible , In addition, you can use  9.png  Stretch to achieve big picture effect   Try not to use the whole big picture .

Strategy

Selectively offer  hdpi,xhdpi,xxhdpi  Picture resources of . It is suggested that priority should be given to  xhdpi  Pictures of the ,  about  mdpi,ldpi  And  xxxhdpi  Provide different parts as needed .

Reuse existing image resources as much as possible . For example, symmetrical pictures , Just provide one , Another picture   Slices can be implemented by code rotation .

The function that can be realized by code drawing , Try not to use too many pictures . For example, reduce the use of multiple picture groups   become  animate-list  Of  AnimationDrawable, This way provides multiple images, which takes up a lot of space .

[](()4、Android  What is symmetric encryption and asymmetric encryption in the way you interact with the server ?

Symmetric encryption , That is, encryption and decryption of data are using the same  key, There are algorithms in this area  DES.  Asymmetric encryption , Encryption and decryption are different  key. Before sending data, make an agreement with the server   Into public and private keys , Data encrypted by public key can be decrypted by private key , conversely . There are algorithms in this area  RSA.ssh  and  ssl  It's all typical asymmetric encryption .

null
[](() 3、 ... and 、Android  Interview FAQs



**1、java  in == and  equals  and  hashCode  The difference between  **

The comparison values of basic data types are equal .  The memory address of the comparison class , Is it the same object , Don't cover  equals  Under the circumstances ,  Compare memory address with , The original realization is  == , Such as  String  Such as rewrite the  equals  Method . hashCode  It's also  Object  class . Returns a discrete  int  Type integer . Operate on the collection class   Use in , To improve query speed .(HashMap,HashSet  Whether the comparison is the same )  If two objects  equals,Java  The runtime environment will think that they  hashcode  Must be equal .  If two objects don't  equals, their  hashcode  It could be equal .  If two objects  hashcode  equal , They don't have to  equals.  If two objects  hashcode  It's not equal , They must not  equals.

2、int  And  integer  The difference between

int  Basic types

integer  object  int  The wrapper class

3、String、StringBuffer、StringBuilder  difference

String: String constant   It doesn't apply to situations where there is a constant need to change the value , Each change is equivalent to a new   The object of

StringBuffer: String variable  ( Thread safety )

StringBuilder: String variable ( Thread unsafe )  Make sure it's available on a single thread , Efficiency is slightly higher than  StringBuffer

4、 What is an inner class ? The role of inner classes

Internal classes can directly access the properties of external classes

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231334474966.html