AndroidのPriorityBlockingQueueでput()メソッドを使う方法を徹底解説
PriorityBlockingQueueとは?
実装例に入る前に、まずPriorityBlockingQueueについて理解しておきましょう。PriorityBlockingQueueは、優先度キュー(PriorityQueue)と同じ順序ルールに従う無制限(unbounded)のブロッキングキューです。要素は自然順序付け、またはコンストラクタで指定したComparatorに基づいて並べ替えられます。
このキューの大きな特徴は、スレッドセーフである点と、容量が固定されていないためメモリ不足(OutOfMemoryError)への対処を考慮した設計になっている点です。マルチスレッド環境で優先度付きのタスク管理を行いたい場合に非常に有用です。
本記事では、AndroidアプリでPriorityBlockingQueueのput()メソッドを使用する方法を、ステップごとに解説します。
put()メソッドの特徴
put()メソッドは、指定した要素をキューの末尾に挿入します。add()メソッドと似ていますが、put()は容量制限のあるブロッキングキューで空きができるまで待機する特性を持つため、挿入操作が確実に行われる点が違いです。
実装手順
ステップ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>
このコードでは、PriorityBlockingQueueの要素を画面に表示するためのTextViewを配置しています。
ステップ3:MainActivity.javaの編集
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.PriorityBlockingQueue;
public class MainActivity extends AppCompatActivity {
PriorityBlockingQueue priorityBlockingQueue;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
priorityBlockingQueue = new PriorityBlockingQueue();
final TextView actionEvent = findViewById(R.id.actionEvent);
priorityBlockingQueue.add("sai");
priorityBlockingQueue.add("ram");
priorityBlockingQueue.add("krishna");
priorityBlockingQueue.add("prasad");
priorityBlockingQueue.add("ram");
actionEvent.setText("" + priorityBlockingQueue);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
priorityBlockingQueue.put("tutorialspoint.com");
actionEvent.setText("" + priorityBlockingQueue);
}
});
}
}
上記のコードでは、まずadd()メソッドを使って複数の文字列をキューに追加しています。その後、TextViewをクリックするとput()メソッドが呼び出され、新しい要素「tutorialspoint.com」がキューに挿入され、更新後のキューの内容が画面に表示される仕組みです。
アプリの実行と結果確認
それでは、アプリを実行してみましょう。実際のAndroid端末をパソコンに接続していることを前提としています。Android Studioからプロジェクトのアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックしてください。実行デバイスとして自分のモバイル端末を選択すると、以下のような初期画面が表示されます。

次に、画面上のTextViewをタップすると、put()メソッドによって要素が追加された結果が以下のように表示されます。

まとめ
PriorityBlockingQueueのput()メソッドを使えば、スレッドセーフな形で優先度キューへ要素を挿入できます。マルチスレッド処理やタスクの優先度管理が必要なAndroidアプリ開発において、ぜひ活用してみてください。
-
AndroidのPriorityBlockingQueueでtake()メソッドを使う方法を徹底解説
PriorityBlockingQueueとは? サンプルに入る前に、まずPriorityBlockingQueueについて理解しておきましょう。PriorityBlockingQueueは容量制限のない(無界)キューで、優先度キューと同じ順序ルールに従います。つまり、要素は自然順序付け、または指定されたComparatorに基づいて自動的に並べ替えられます。 このキュー最大の特徴はブロッキング機能を持っている点です。take()メソッドを呼び出したときにキューが空であっても、要素が追加されるまでスレッドが自動的に待機するため、メモリ不足(Out of Memory)エラーへの対処にも役立ちま
-
Androidローダーの使い方とは?連絡先一覧を表示する実装例をステップ解説
Androidローダー(Loader)とはローダーは、Android 3.0(APIレベル11)で導入された、バックグラウンドで非同期にデータを読み込むための仕組みです。UIスレッドをブロックすることなくコンテンツプロバイダやデータベースからデータを取得でき、画面回転などの設定変更時にも読み込み済みのデータを再利用できるのが大きな特徴です。本記事では、ローダー(CursorLoader)を使って端末の連絡先一覧をListViewに表示する方法を、サンプルコード付きでステップごとに解説します。ステップ1:新しいプロジェクトを作成するAndroid Studioを起動し、メニューから「File」⇒