ArrayBlockingQueue में size() मेथड का उपयोग
ArrayBlockingQueue एक निश्चित क्षमता वाला बाउंडेड क्यू है। size() वर्तमान एलिमेंट्स की संख्या रिटर्न करता है।
चरण 1: प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2: लेआउट (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:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/actionEvent"
android:textSize="40sp"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
चरण 3: MainActivity.java - size() डेमो
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.os.Bundle;
import androidx.appcompat.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("साई");
arrayBlockingQueue.add("राम");
arrayBlockingQueue.add("कृष्ण");
arrayBlockingQueue.add("प्रसाद");
actionEvent.setText("" + arrayBlockingQueue);
actionEvent.setOnClickListener(v -> {
@SuppressLint("SetTextI18n")
int size = arrayBlockingQueue.size();
actionEvent.setText("आकार: " + size);
});
}
}
ArrayBlockingQueue क्षमता संबंधी मेथड्स:
| मेथड | विवरण |
|---|---|
size() | वर्तमान एलिमेंट्स की संख्या |
remainingCapacity() | शेष क्षमता (क्षमता - आकार) |
capacity() | कुल क्षमता (कंस्ट्रक्टर में सेट) |
isEmpty() | true अगर size == 0 |
उदाहरण:
क्षमता = 50 वर्तमान आकार = 4 remainingCapacity() = 46
आउटपुट
प्रारंभ: [साई, राम, कृष्ण, प्रसाद]
क्लिक के बाद: आकार: 4


नोट: size() थ्रेड-सेफ है लेकिन तुरंत बदल सकता है (कमजोर संगति)।