ArrayBlockingQueue क्या है?
ArrayBlockingQueue एक बाउंडेड ब्लॉकिंग क्यू है जो FIFO तरीके से काम करती है। पहला एलिमेंट सबसे लंबे समय तक रहता है और अंतिम एलिमेंट सबसे कम समय तक।
उद्देश्य
यह उदाहरण दर्शाता है कि Android में ArrayBlockingQueue में poll() मेथड का उपयोग कैसे करें।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: लेआउट XML जोड़ें
निम्न कोड को 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>
ऊपर के कोड में, हमने ArrayBlockingQueue एलिमेंट्स दिखाने के लिए एक TextView लिया है।
चरण 3: MainActivity जावा कोड जोड़ें
निम्न कोड को 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;
@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) {
actionEvent.setText("" + arrayBlockingQueue.poll());
}
});
}
}
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें:

अब TextView पर क्लिक करके परिणाम देखें (हेड एलिमेंट दिखेगा):

poll() मेथड के बारे में
- यह मेथड क्यू के हेड (पहले एलिमेंट) को हटाकर लौटाता है।
- यदि क्यू खाली है, तो यह
nullलौटाता है। - यह नॉन-ब्लॉकिंग ऑपरेशन है (ब्लॉकिंग के लिए
take()उपयोग करें)। - ArrayBlockingQueue बाउंडेड होने के कारण क्षमता सीमित होती है।