ConcurrentLinkedDeque란 무엇인가?
ConcurrentLinkedDeque는 연결 리스트(linked nodes) 기반으로 구현된 크기 제한이 없는(unbounded) 덱(Double Ended Queue)입니다. 내부적으로 잠금 없는(lock-free) 알고리즘을 사용하기 때문에 여러 스레드가 동시에 요소에 접근하더라도 안전하게 동작합니다. 따라서 멀티스레드 환경에서 작업 큐나 공유 데이터 구조가 필요할 때 유용하게 활용할 수 있습니다.
이번 예제에서는 Android에서 ConcurrentLinkedDeque의 peekLast() 메서드를 사용하는 방법을 알아보겠습니다.
peekLast() 메서드의 역할
peekLast()는 덱의 마지막(꼬리) 요소를 조회만 하고 제거하지 않는 메서드입니다. 덱이 비어 있는 경우에는 예외를 발생시키지 않고 null을 반환한다는 점이 특징입니다. 이와 비슷한 메서드로 마지막 요소를 조회하면서 제거하는 pollLast(), 그리고 비어 있을 때 예외를 던지는 getLast()가 있습니다.
구현 단계
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단계 — MainActivity 코드 작성
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.view.View;
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);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
actionEvent.setText("" + concurrentLinkedDeque.peekLast());
}
});
}
}위 코드에서는 ConcurrentLinkedDeque에 "sai", "ram", "krishna", "prasad" 네 개의 문자열을 순서대로 추가한 뒤, 전체 덱의 내용을 TextView에 출력했습니다. 그리고 TextView를 클릭하면 peekLast()를 호출하여 덱의 마지막 요소를 화면에 다시 표시하도록 클릭 리스너를 설정했습니다.
애플리케이션 실행 및 결과 확인
실제 Android 기기를 컴퓨터에 연결했다고 가정하고 애플리케이션을 실행해 보겠습니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 열고 툴바의 Run 아이콘을 클릭한 후, 옵션 목록에서 자신의 모바일 기기를 선택하세요.
앱이 실행되면 기본 화면에 덱에 저장된 전체 요소가 표시됩니다.

이제 화면의 TextView를 클릭하면 peekLast()가 반환한 마지막 요소인 prasad가 아래와 같이 표시됩니다.

정리
peekLast()는 ConcurrentLinkedDeque의 마지막 요소를 안전하게 확인할 수 있는 간편한 메서드입니다. 요소를 제거하지 않으면서 꼬리 값을 조회해야 하는 상황에서 활용하면 되며, 덱이 비어 있을 때 null 반환 처리만 주의하면 됩니다.