Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में ConcurrentLinkedQueue में poll() का उपयोग कैसे करें?


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 आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और डिफ़ॉल्ट स्क्रीन देखें:

Android में ConcurrentLinkedQueue में poll() का उपयोग कैसे करें?

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

Android में ConcurrentLinkedQueue में poll() का उपयोग कैसे करें?

poll() मेथड के बारे में

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

    ConcurrentLinkedQueue क्या है? ConcurrentLinkedQueue एक अनबाउंडेड क्यू है जो लिंक्ड नोड्स पर आधारित है। मल्टीपल थ्रेड्स इसके एलिमेंट्स को थ्रेड-सेफ्टी के साथ एक्सेस कर सकते हैं। एलिमेंट्स FIFO स्ट्रेटेजी के अनुसार ट्रैवल करते हैं और टेल से इन्सर्ट होते हैं। यह null वैल्यूज को अनुमति नहीं देता। उद्

  1. एंड्रॉइड लोडर का उपयोग कैसे करें? एंड्रॉइड लोडर का उपयोग कैसे करें?

    एंड्रॉइड में लोडर (लीगेसी) बनाम आधुनिक विकल्प लोडर (android.app.LoaderManager, CursorLoader) API 28 में डिप्रिकेट हो गए हैं। नए कोड के लिए, ViewModel + LiveData/Flow + Room/Paging 3 या Coroutines + Flow का उपयोग करें। यह लेख लीगेसी लोडर API को संदर्भ के लिए दस्तावेज करता है और आधुनिक प्रतिस्थापन दिख

  1. LocalBroadcastManager का उपयोग कैसे करें? LocalBroadcastManager का उपयोग कैसे करें?

    LocalBroadcastManager - ऐप-आंतरिक इवेंट बस नोट: LocalBroadcastManager **डिप्रिकेट** है (AndroidX 1.1.0+ से)। आधुनिक ऐप्स को **LiveData/Flow**, **RxJava**, या **इवेंट बस लाइब्रेरी (greenrobot EventBus, Kotlin Channel/Flow)** का उपयोग करना चाहिए। यह लेख लीगेसी API को दस्तावेज करता है और माइग्रेशन पथ द