ArrayBlockingQueue क्या है?
ArrayBlockingQueue FIFO तरीके से काम करता है और पहला एलिमेंट सबसे लंबे समय तक रहता है जबकि क्यू का अंतिम एलिमेंट कम समय तक रहता है।
उदाहरण: take() मेथड का उपयोग
यह उदाहरण दर्शाता है कि Android में ArrayBlockingQueue में take() मेथड का उपयोग कैसे किया जाता है।
चरण 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>
चरण 3: src/MainActivity.java में कोड जोड़ें
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.util.concurrent.ArrayBlockingQueue;
public class MainActivity extends AppCompatActivity {
ArrayBlockingQueue<String> arrayBlockingQueue;
String head;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayBlockingQueue = new ArrayBlockingQueue<>(50);
final TextView actionEvent = findViewById(R.id.actionEvent);
arrayBlockingQueue.add("sai");
arrayBlockingQueue.add("ram");
arrayBlockingQueue.add("krishna");
arrayBlockingQueue.add("prasad");
actionEvent.setText("" + arrayBlockingQueue);
actionEvent.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
try {
head = arrayBlockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
actionEvent.setText("" + arrayBlockingQueue);
}
});
}
}
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफॉल्ट स्क्रीन देखें।

अब TextView पर क्लिक करें, यह पहला एलिमेंट हटाकर शेष एलिमेंट्स दिखाएगा:
