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 कैसे काम करता है?
- Background Thread 3 सेकंड तक wait करता है।
- runOnUiThread() का उपयोग करके textView को UI thread पर update किया जाता है।
- UI freeze नहीं होती, क्योंकि update background thread में होता है और सिर्फ UI change UI thread में होता है।
Related Questions (FAQ in Hindi)
- AsyncTask deprecated क्यों हुई?
- Coroutines और AsyncTask में क्या अंतर है?
- LiveData कैसे UI को update करता है?
runOnUiThread()कब use करना चाहिए?- Background thread और UI thread में क्या फर्क होता है?
Request
Leave a Comment
INITIALIZING...
INITIALIZING...
Comments Total: 0 comments