runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("Updated on UI Thread");
}
});
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 किया जा सकता है।
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) का उपयोग background में task करने और उसके result को UI thread पर update करने के लिए किया जाता था।
Coroutines (Kotlin) और LiveData (MVVM Architecture) modern तरीके हैं, जिनसे UI thread को सुरक्षित तरीके से update किया जाता है।
अगर कोई long-running task UI thread पर execute होता है, तो पूरी application freeze हो सकती है, इसलिए background thread का उपयोग करना जरूरी है।
Proper thread management से app की performance और user experience बेहतर होता है।
नीचे एक simple example दिया गया है, जिसमें background thread से UI thread पर update किया जाता है।
Example: runOnUiThread() का उपयोग
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 में होता है।
Request
अगर आपको यह article useful या interesting लगा हो, तो please इसे अपने dosto aur family ke साथ जरूर share करें। आपका एक छोटा सा कदम हमें और अच्छा content बनाने के लिए motivate करता है। Thank you!
ध्यान दें कि इस page पर आपको कुछ ads भी देखने को मिल सकते हैं। इसके लिए हम आपसे माफी चाहते हैं। हम इस content को तैयार करने में काफी मेहनत और time लगाते हैं, ताकि आपको valuable जानकारी मिल सके। इन्हीं ads की मदद से हम ये काम continue कर पाते हैं।
आपके support और understanding के लिए दिल से धन्यवाद।