brunch

You can make anything
by writing

C.S.Lewis

by 이승현 May 31. 2016

안드로이드 면접 질문 #04

Android interview questions

Skillgun


Android threads interview questions and answers


http://skillgun.com/android/activities/interview-questions-and-answers/paper/13


* 갑자기 사이트가 안 들어가지네요ㅠ


Question #01

Every application will have by default one thread. True or false?

(모든 어플리케이션은 기본적으로 하나의 스레드를 가지고 있나요?)

                              

   

Answer

- Default android will allocate one main thread called as (UI thread) to every process or application.

(기본적으로 안드로이드는 모든 프로세스나 어플리케이션에 하나의 메인 스레드(UI 스레드)를 할당한다.)


Question #02

Main thread will have a looper. True or false?

(메인 스레드는 looper를 가지고 있나요?)

                              

   

Answer

- Main Thread is a handler thread, so it will have loooper enabled. Normal threads looper will be in disabled mode, where as handler threads will have their loopers enabled. but if we want we can prepare loopers for normal threads also.
(메인 스레드는 핸들러 스레드라서 활성화된 루퍼를 가지고 있다. 일반 스레드 루퍼는 비활성화 모드가 되고 핸들러 스레드는 활성화된 루퍼를 가지고 있다. 그러나 원한다면 일반 스레드를 위한 루퍼를 준비할 수 있다.)


Question #03

How Android inter thread communication works?

(어떻게 안드로이드에서 스레드 간 내부 통신이 동작하는가?)

                              


 

Answer

- Inter thread communication : means passing some data between threads.

(스레드 간 데이터를 주고받는 것을 의미한다.)


Every main thread internally will have 2 components: Looper & Message queue.

(모든 메인 스레드는 내부적으로 루퍼와 메시지 큐라는 2가지 구성요소를 가지고 있다.)


Message queue: is to store incoming messages.

(메시지 큐는 수신 메시지를 저장하고 있다.)


Looper : will keep checking message queue to response to those messages.

With out Looper, it is not possible to achieve Inter thread communication.

Only Handler Threads will have loopers.

(루퍼는 수신 메시지에 응답하기 위해 메시지 큐를 항상 체크하고 있다.)

(루퍼 없이, 스레드 간 내부 통신은 불가능하다. 오직 핸들러 스레드만이 루퍼를 가지고 있다.)


Normal Java threads will also have loopers but they will be in the passive mode. (Since looper is passive it cannot check message queue for new incoming message).

(일반 자바 스레드는 수동 모드 일 때 루퍼를 가질 수 있다.(루퍼는 수동적이기 때문에 새로운 수신 메시지에 대한 메시지 큐를 확인할 수 없습니다.))


Since your looper is not active in java thread so they cannot handle inter thread communication. You can enable the looper by calling looper.prepare().

(루퍼는 자바 스레드에선 비활성화 상태이기 때문에 스레드 간 내부 통신을 처리할 수 없다. looper.prepare()를 호출해 루퍼를 활성화시킬 수 있다.)


So, Inter thread communication is possible through only Handler Threads. Because Handler threads allows message passing mechanism through loopers.

(그래서 스레드 간 내부 통신은 오직 핸들러 스레드를 통해서만 가능하다. 왜냐면 핸들러 스레드는 루퍼를 통한 메시지 전달 메커니즘을 허용하기 때문이다.)


There is one more technique to have Inter thread communication. Share some data in some common storage, and allow both the threads to access that data by using some synchronization mechanism like wait, notify. This is possible with normal java threads also.

(스레드 간 내부 통신을 위한 기술이 하나 더 있다. 데이터를 일반 저장소에 공유하고 wait, notify 같은 동기화 메커니즘을 이용해 두 개의 스레드가 이 데이터에 접근하는 것을 허용하기이다. 이 것은 일반 자바 스레드에서도 가능하다.)


Question #04

What is the difference between task, process, application. and thread?

(task, process, application, thread 차이?)

                              

   

Answer

- Process : every instance of an application being executed is called as process. You execute same application two times, it will create 2 processes.

(실행된 모든 애플리케이션의 인스턴스를 프로세스라 부른다. 동시에 같은 애플리케이션을 실행하면 2개의 프로세스를 생성한다.)


- Application : is generalized term of a process.

(애플리케이션은 프로세스의 일반화된 용어이다.)


- Task : can have one or more applications in it.

(Task는 1개 이상의 애플리케이션을 가질 수 있다.)


eg : task of sending a message : we will start with message application, lets say i want to attach a photo to my message, then i will open gallery application also. In this case my task has got two applications, messaging and gallery application.

(예를 들어 메시지를 보내는 task : 메시지 어플리케이션을 시작하고, 사진이 첨부된 메시지를 보내고 싶을 때, 갤러리 어플리케이션 또한 오픈할 것이다. 이 경우 내 task는 메세징과 갤러리 2개의 어플리케이션을 가진다.)


- Thread : theoretically thread is a light weight process, or part of process. Practically thread is dispatch-able unit to CPU, and it is internal part of a process. With out a thread in your program it is difficult to execute your application. That's why by default every process will have at least one thread created by Operating system.

(이론적으로 스레드는 경량화된 프로세스, 또는 프로세스의 일부분이다. 실제로 스레드는 CPU의 dispatch-able 한 단위이고, 프로세스의 내부 부분이다. 스레드가 없는 프로그램은 어플리케이션을 실행하기 어렵다. 기본적으로 모든 프로세스는 OS에 의해 생성된 적어도 하나 이상의 스레드를 가지고 있기 때문이다.)


* 프로세스 : 프로그램 실행 단위. / 고유의 메모리를 가지고 있다. / 여러 개의 스레드를 가질 수 있다.

* 스레드 : 프로그램 실행 흐름. / 메모리를 공유하고 있다. / 하나의 프로세스에 종속되어 있다.


Question #05

Does android support multitasking? How multitasking works in android, explain how to start a new task when you are already running a task?

(안드로이드는 멀티태스킹을 지원하나요? 이미 실행 중인 task가 있을 때 어떻게 새로운 task를 시작하는지 설명해주세요.)

                              

   

Answer

- Android supports multitasking at app level also. That means you can create apps with multitasking capability. One way to achieve multitasking is press home button on current task which will move it to background and then you can start new task from launcher. Another way is use FLAG_NEW_TASK in the intent, when you are starting a new activity.

(안드로이드는 app 레벨에서도 멀티태스킹을 지원합니다. 즉 멀티태스킹 기능을 가진 앱을 만들 수 있음을 의미합니다. 한 가지 방법은 홈버튼을 눌러 현재 task를 background로 옮기고 런처를 통해 새로운 task를 실행합니다. 다른 방법은 새로운 액티비티를 시작할 때, 인텐트의 FLAG_NEW_TASK를 이용한다.)


Question #06

Does android support multi threading?

(안드로이드는 멀티 스레딩을 지원하나요?)

                              

   

Answer

- Yes it supports both multitasking and multi-threading. User can launch multiple applications at a time and can switch between those applications (this is called as multitasking). In a given application we can have multiple threads which can runs simultaneously (this is called as multi-threading).

(예 멀티태스킹과 멀티스레딩 모두 지원합니다. 유저는 동시에 여러 개의 어플리케이션을 실행할 수 있고 이 애플리케이션들을 전환할 수 있습니다.(멀티태스킹). 어플리케이션은 여러 개의 스레드들을 가질 수 있고 동시에 실행할 수 있습니다.(멀티스레딩))


Question #07

What is the mechanism used by android for Inter-process-communication?

(IPC를 위해 안드로이드가 이용하는 메커니즘은?)

                              

   

Answer

- IPC means Inter process communication : Where two applications or processes will communicate with each other by passing some data between them. Since android is meant for embedded and small devices, we should not use serialization for IPC, rather we can use BINDERs which internally uses parcels, parcel is a sort of light weight serialization by using shared memory concept.

(IPC는 프로세스 간 내부 통신을 의미합니다. 두 개의 어플리케이션이나 프로세스는 서로 데이터를 주고받으며 통신할 수 있습니다. 안드로이드는 작고 embedded 기기를 의미하기 때문에 IPC를 위해 오히려 직렬화를 이용하지 않고 공유 메모리 개념을 이용하는 경량 직렬화의 일종인 parcel를 내부적으로 이용한 BINDER를 이용해야 한다.)


There are many differences between Binder IPC and Serialization IPC:

(바인더 IPC와 직렬화 IPC 간 많은 차이점이 있다.)


1. Serialization is very heavy to use in embedded devices, communication will be very slow.

(직렬화는 임베디드 기기에서 쓰기엔 무겁고 통신이 느리다.)


2. Binders uses Parcels to make IPC very fast.

(바인더는 Parcel를 이용해 IPC가 매우 빠르다.)


3. Binders internally uses Shared memory concept which uses less memory while sharing data between two processes.

(바인더는 두 개의 프로세스 간 데이터를 공유할 때 내부적으로 적은 메모리를 이용하는 공유 메모리 개념을 이용한다.)


Bottom line : Binders uses less memory, and quite fast as it uses parcels. Serialization is very heavy, takes time to send and receive data, and also takes more memory compared to binder.

(결론 : 바인더는 parcel을 이용해 메모리를 더 적게 이용하고 굉장히 빠르다. 직렬화는 데이터를 주고받는데 시간이 오래 걸리고 무겁다 또한 바인더에 비해 더 많은 메모리를 소모한다.)


Note : To pass data between activities, services, and receivers use only Bundles. Don't go for either serialization or binders. Binders are specifically used only for binder services where 2 processes will communicate.

(액티비티, 서비스 그리고 리시 버간 데이터를 전달하기 위해 Bundle만 이용한다. 직렬화나 바인더는 이용하지 마라. 바인더는 두 개의 프로세스 간 통신만을 위해 이용하기 때문이다.)


Question #08

How to avoid synchronization problems in threads?

(스레드에서 동기화 문제를 회피하는 방법?)

                              

   

Answer

- to avoid synchronized problems with multi threaded programs, use synchronized block or synchronized methods, which ever suits better.

(멀티 스레드 프로그램에서 동기화 문제를 회피하기 위해, synchronized block 또는 synchronized methods 중 더 적당한 것을 이용한다.)


Question #09

Is it possible to start asyncTask from background thread?

(background 스레드에서 asyncTask를 시작할 수 있나?)

                              

   

Answer

- it is mandatory that one has to call asyncTask only from main thread, else it may crash at run time when we try to touch UI from onPreExecute or onProgressUpdate or onPostExecute functions.

(asyncTask는 메인 스레드에서만 호출하는 것이 필수이고, 만약 그렇지 않다면 onPreExecute, onProgressUpdate, onPostExecute 함수에서 UI를 터치하려 한다면 충돌이 일어날 것이다.)


Question #10

What will happen if execute() function of asynctask is called more than once, for a give asynctask object?

(asynctask의 execute() 함수를 여러 번 호출한다면?)

                              

   

Answer

- We should not call execute() function more than once on a given asynctask object, it will throw run time exception.

(asynctask 객체의 execute() 함수를 여러 번 호출한다면 런타임 예외가 발생할 것이다.)


Question #11

What are the mandatory functions to be implemented in AsyncTask class?

(AsyncTask 클래스를 구현할 때 필수 함수는?)

                              

   

Answer

- doInBackground() is the only mandatory function that has to be implemented in AsyncTask class.

(doInBackground() 함수를 필수적으로 구현해야 한다.)


Question #11

How many threads will be created with asynctask?

(asynctask에서 얼마나 많은 스레드를 만들 수 있나?)

                              

   

Answer

- till donut : it is used to create single thread, 

- from 1.6 to 2.3 : it is used to create multi threads,

- from 3.0 on wards : it is again used to create single thread. If you want to create multible threads with asynctask from 3.0 on wards, instead of using execute(), use executeOnExecutor(Execute e, variable). They have reverted back the basic functionality if asynctask.execute() to behave as a single threaded, because multi threaded programming will lead to lot of complications in terms of synchronization.


* 1.0 ~ 1.5 : 단일 스레드

* 1.6 ~ 2.3 : 멀티 스레드

* 3.0 ~      : 단일 스레드 

(3.0 이상 버전에서부터 멀티 스레드를 생성하려면, execute() 대신 executeOnExcutor()를 이용해라. 멀티 스레드 프로그래밍은 동기화 관점에서 많은 문제를 야기시킬 수 있기 때문에 asynctask.execute()를 다시 단일 스레드로 동작으로 되돌려 놓았다.)


Question #12

Define AsyncTask? How many methods are there and how parameters work in it?

(Asynctask? 어떤 메소드와 어떤 파라미터 값들이 어떻게 동작?)

                              

   

Answer

- The AsyncTask class is a way to achieve multithreading in android application, where multiple tasks can be run at a time. It synchronizes with the main thread. It also supports reporting progress of the running tasks.

(AsyncTask 클래스는 한 번에 여러 개의 task가 실행될 수 있는 안드로이드 어플리케이션 환경에서 멀티스레딩을 구현할 수 있는 한 가지 방법이다. 메인 스레드와 동기화한다. 또한 실행 중인 작업의 상황을 보고하는 기능도 지원한다.)


There are four methods in AsyncTask:

1. onPreExecute()

2. doInBackgroud()

3. onUpdateProgress()

4. onPostExecute()


The three types of parameters in AsyncTask:

1. Params : the type of the parameters sent to the task upon execution.

(실행 시 task에 전송될 파라미터 타입)


2. Progress : the type of the progress units published during the background computation.

(백그라운드 동작의 진행상황 타입)


3. Result : the type of the result of the background computation.

(백그라운드 동작의 결과 타입)





매거진의 이전글 안드로이드 면접 질문 #03
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari