Menu

Related Topics

HTML CSS JavaScript PHP Node.js React.js C C++ Go Rust Swift Python Java Excel MS Word PowerPoint
Search

Running Threads on UI Threads (Run on UI Threads) in Hindi

Running Threads on UI Threads

UI Thread (Main Thread) Android में मुख्य thread होता है, जो UI components को handle करता है।

Run on UI Thread का मतलब है कि कोई भी code या task सीधे UI thread पर execute होगा।

UI updates जैसे TextView update, Button click events, Toast message, Dialog box open करना आदि केवल UI thread से ही किए जा सकते हैं।

यदि कोई heavy operation (network call, database query, file processing) UI thread पर execute किया जाए, तो app slow हो सकती है या ANR (Application Not Responding) error आ सकता है।

Android में runOnUiThread() method का उपयोग करके किसी भी background thread से UI thread पर task execute किया जा सकता है।

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        textView.setText("Updated on UI Thread");
    }
});

Handler & Looper का उपयोग भी background thread से UI thread पर task भेजने के लिए किया जाता है।

new Handler(Looper.getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        textView.setText("Updated using Handler");
    }
});

AsyncTask (जो अब deprecated हो चुकी है) का उपयोग Android में background tasks करने के लिए किया जाता था, जैसे network calls या heavy computation — और task पूरा होने पर result को UI Thread पर update किया जाता था।

अब Android development में Coroutines (Kotlin) और LiveData जैसे modern विकल्प उपलब्ध हैं, जो ज्यादा efficient और readable हैं।

अगर कोई long-running task (जैसे API call, file upload, या database query) सीधे UI thread पर चले, तो app freeze या ANR (App Not Responding) error दिखा सकती है।

इसलिए background threads का use करना जरूरी होता है, ताकि UI thread smooth रहे और user को lag feel ना हो।

Thread management को ठीक से handle करना app की performance, battery usage और user experience को बेहतर बनाता है।

Example: runOnUiThread() का उपयोग

नीचे एक simple example है जिसमें background thread से result लेकर UI thread को update किया जा रहा है:

public class MainActivity extends AppCompatActivity {
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);

        // Background thread शुरू करें
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000); // 3 सेकंड का delay
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // UI thread पर update करें
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        textView.setText("Updated on UI Thread!");
                    }
                });
            }
        }).start();
    }
}

Code कैसे काम करता है?

Related Questions (FAQ in Hindi)

  1. AsyncTask deprecated क्यों हुई?
  2. Coroutines और AsyncTask में क्या अंतर है?
  3. LiveData कैसे UI को update करता है?
  4. runOnUiThread() कब use करना चाहिए?
  5. Background thread और UI thread में क्या फर्क होता है?


Page Views
0
Visit ID
Not tracked yet
Visit Time
-
Time on Page
0 seconds

Request


Leave a Comment


INITIALIZING...


INITIALIZING...


Quizzes in Hindi

GK Online Quiz in Hindi 10 pages
HTML Online Quiz in Hindi 10 pages
CSS Online Quiz in Hindi 10 pages
JavaScript Online Quiz in Hindi 10 pages
CCC Online Quiz in Hindi 10 pages
History Online Quiz in Hindi 10 pages



Comments Total: 0 comments

📰 Latest Articles

Total: 0 Articles

Featured Products

0 Products 0 Loaded 0 Remaining

All Products Loaded!

You've reached the end of our collection.

Contact Us Form