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

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


ConcurrentLinkedQueue क्या है?

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

उदाहरण: isEmpty() मेथड का उपयोग

यह उदाहरण दर्शाता है कि Android में ConcurrentLinkedQueue के isEmpty() मेथड का उपयोग कैसे करें।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनें।

चरण 2: लेआउट फ़ाइल (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>

चरण 3: 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;

    @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);

        // क्लिक पर isEmpty() चेक करें
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean empty = concurrentLinkedQueue.isEmpty();
                actionEvent.setText("" + empty);
            }
        });
    }
}

ऐप चलाएं और परीक्षण करें

ऐप चलाएं। प्रारंभ में कतार के तत्व दिखेंगे (isEmpty() false लौटाएगा)। TextView पर क्लिक करने पर false प्रदर्शित होगा। यदि कतार खाली होती तो true लौटाता।

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

क्लिक करने के बाद परिणाम:

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

मुख्य बिंदु

  • isEmpty() मेथड जांचता है कि कतार खाली है या नहीं।
  • खाली होने पर true, अन्यथा false लौटाता है।
  • ConcurrentLinkedQueue लॉक-फ्री और थ्रेड-सेफ है।
  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 को दस्तावेज करता है और माइग्रेशन पथ द