ConcurrentLinkedQueue क्या है?
ConcurrentLinkedQueue एक अनबाउंडेड क्यू है जो लिंक्ड नोड्स पर आधारित है। मल्टीपल थ्रेड्स इसके एलिमेंट्स को थ्रेड-सेफ्टी के साथ एक्सेस कर सकते हैं। एलिमेंट्स FIFO स्ट्रेटेजी के अनुसार ट्रैवल करते हैं और टेल से इन्सर्ट होते हैं। यह null वैल्यूज को अनुमति नहीं देता।
उद्देश्य
यह उदाहरण दर्शाता है कि Android में ConcurrentLinkedQueue में 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>
ऊपर के कोड में, हमने ConcurrentLinkedQueue एलिमेंट्स दिखाने के लिए एक TextView लिया है।
चरण 3: MainActivity जावा कोड जोड़ें
निम्न कोड को 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.ConcurrentLinkedQueue;
public class MainActivity extends AppCompatActivity {
ConcurrentLinkedQueue<String> concurrentLinkedQueue;
String head;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
final TextView actionEvent = findViewById(R.id.actionEvent);
concurrentLinkedQueue.add("sai");
concurrentLinkedQueue.add("ram");
concurrentLinkedQueue.add("krishna");
concurrentLinkedQueue.add("prasad");
concurrentLinkedQueue.add("ram");
actionEvent.setText("" + concurrentLinkedQueue);
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
concurrentLinkedQueue.poll();
actionEvent.setText("" + concurrentLinkedQueue);
}
});
}
}
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें:

अब TextView पर क्लिक करके परिणाम देखें:

poll() मेथड के बारे में
- यह मेथड क्यू के हेड (पहले एलिमेंट) को हटाकर लौटाता है।
- यदि क्यू खाली है, तो यह
nullलौटाता है (कोई एक्सेप्शन नहीं)। - यह नॉन-ब्लॉकिंग, थ्रेड-सेफ ऑपरेशन है।
- FIFO ऑर्डर के अनुसार पहला एलिमेंट हटाता है।