예제를 살펴보기 전에 ConcurrentLinkedDeque가 무엇인지 먼저 알아보겠습니다. ConcurrentLinkedDeque는 연결 노드(linked nodes) 기반의 크기 제한이 없는(unbounded) 덱(deque) 자료구조로, 여러 스레드가 동시에 요소에 접근하더라도 안전하게 처리할 수 있도록 설계되었습니다.
이 예제에서는 Android에서 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단계: 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.getFirst());
}
});
}
}
위 코드에서는 add() 메서드를 사용해 덱에 여러 요소를 추가한 후, TextView를 클릭하면 getFirst() 메서드를 호출하여 덱의 첫 번째 요소를 화면에 표시하도록 구현했습니다.
애플리케이션 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 Android 모바일 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 앱을 실행하려면 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바에서 Run 아이콘을 클릭합니다. 그다음 모바일 기기를 선택하면, 기기의 기본 화면에 아래와 같은 결과가 표시됩니다.

이제 화면의 TextView를 클릭하면 아래와 같이 덱의 첫 번째 요소가 결과로 표시됩니다.
