ArrayBlockingQueue में clear() मेथड से सभी एलिमेंट्स हटाएं
clear() क्यू के सभी एलिमेंट्स को एटॉमिकली हटा देता है, क्यू को खाली स्थिति में छोड़ता है।
चरण 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 - clear() डेमो
package com.example.myapplication;
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 -> {
arrayBlockingQueue.clear();
actionEvent.setText("क्लियर के बाद: " + arrayBlockingQueue);
});
}
}
clear() व्यवहार:
- एटॉमिक: सभी एलिमेंट्स एक साथ हटते हैं
- वेटिंग थ्रेड्स:
put()पर ब्लॉक्ड प्रोड्यूसर्स अनब्लॉक होते हैं (स्पेस उपलब्ध) take()पर ब्लॉक्ड कंज्यूमर्स कोNoSuchElementExceptionनहीं - वे इंतजार करते रहेंगे- क्षमता अपरिवर्तित:
capacity()वही रहता है,size()0 हो जाता है
संबंधित मेथड्स:
| मेथड | विवरण |
|---|---|
clear() | सभी एलिमेंट्स हटाएं |
remove(Object) | विशिष्ट एलिमेंट हटाएं (पहला मिलान) |
drainTo(Collection) | एलिमेंट्स दूसरे कलेक्शन में ले जाएं |
आउटपुट
पहले: [साई, राम, कृष्ण, प्रसाद]
क्लिक के बाद: क्लियर के बाद: [] (खाली)

नतीजा: TextView खाली क्यू दिखाता है []।