ConcurrentLinkedDeque란 무엇인가?
예제를 살펴보기 전에 먼저 ConcurrentLinkedDeque에 대해 간단히 알아보겠습니다. ConcurrentLinkedDeque는 연결 노드(linked nodes) 기반의 무제한(unbounded) 덱(Double Ended Queue)으로, 여러 스레드가 동시에 요소에 접근해도 안전하게 동작하는 스레드 세이프(thread-safe) 자료구조입니다. 즉, 별도의 동기화 처리 없이도 멀티스레드 환경에서 양쪽 끝으로 데이터를 삽입하거나 제거할 수 있습니다.
이번 글에서는 안드로이드에서 ConcurrentLinkedDeque를 생성하고, 요소를 추가한 뒤 화면에 출력하는 과정을 단계별로 알아보겠습니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File → New Project로 이동하여 새 프로젝트를 생성하고, 필요한 정보를 모두 입력합니다.
2단계 — 레이아웃 코드 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/actionEvent"
android:textSize="40sp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
위 코드에서는 ConcurrentLinkedDeque의 요소들을 화면에 표시하기 위해 TextView를 하나 배치했습니다.
3단계 — 메인 액티비티 코드 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다.
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.concurrent.ConcurrentLinkedDeque;
public class MainActivity extends AppCompatActivity {
ConcurrentLinkedDeque concurrentLinkedDeque;
String head;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
concurrentLinkedDeque = new ConcurrentLinkedDeque<String>();
final TextView actionEvent = findViewById(R.id.actionEvent);
concurrentLinkedDeque.add("sai");
concurrentLinkedDeque.add("ram");
concurrentLinkedDeque.add("krishna");
concurrentLinkedDeque.add("prasad");
actionEvent.setText("" + concurrentLinkedDeque);
}
}
위 코드에서는 add() 메서드를 사용해 "sai", "ram", "krishna", "prasad" 네 개의 문자열을 순서대로 덱에 추가한 후, setText()를 통해 전체 덱 내용을 TextView에 출력합니다.
4단계 — 앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바에서 Run(실행) 아이콘을 클릭하세요. 옵션 목록에서 본인의 모바일 기기를 선택하면, 기기 화면에 다음과 같은 기본 화면이 표시됩니다.
