Java का Runnable Interface
In this page:
class TaskName implements Runnable {
public void run() {
// task code
}
}
Thread thread = new Thread(new TaskName());
thread.start();
What is the Runnable Interface?
Runnable interface Java में एक functional interface है जो उस task को represent करने के लिए उपयोग होता है जिसे कोई thread execute कर सकता है। यह एक single abstract run() method define करता है जिसमें वे statements होते हैं जो background में चलने चाहिए, यह task असल में किसी thread पर कैसे schedule होता है उससे स्वतंत्र।
उदाहरण: What is the Runnable Interface?
// Define the class Main
public class Main {
// Define the class Task
static class Task implements Runnable {
public void run() {
// Print a line to the console
System.out.println("Task executing");
}
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
new Task().run();
}
}
Login to try C/C++/Java/PHP code in the editor
Implementing Runnable
Runnable implement करने के लिए, अपनी class पर implements keyword का उपयोग करें और इसके run() method के लिए एक body प्रदान करें। यह 'क्या काम होना चाहिए' को 'यह कैसे execute होता है' से अलग करता है, क्योंकि एक ही Runnable object को किसी Thread, किसी ExecutorService, या किसी भी चीज़ को सौंपा जा सकता है जो इसे चलाना जानती है।
उदाहरण: Implementing Runnable
// Define the class Main
public class Main {
// Define the class PrintTask
static class PrintTask implements Runnable {
public void run() {
// Print a line to the console
System.out.println("Work happens here");
}
}
// Program entry point: the JVM starts running here
public static void main(String[] args) throws InterruptedException {
// Create a new Thread object and store it in t
Thread t = new Thread(new PrintTask());
// Start the thread; it runs in parallel with this code
t.start();
// Wait for the thread to finish
t.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Creating Threads with Runnable
किसी Runnable task को असल में चलाने के लिए, सीधे Thread को extend करने के बजाय अपने task instance को Thread constructor में पास करें। यह एक Thread object बनाता है जो सिर्फ आपके Runnable के run() method को wrap और delegate करता है जब start() call होता है।
उदाहरण: Creating Threads with Runnable
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws InterruptedException {
Runnable task = () -> System.out.println("Runnable passed to Thread");
// Create a new Thread object and store it in t
Thread t = new Thread(task);
// Start the thread; it runs in parallel with this code
t.start();
// Wait for the thread to finish
t.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Lambda Expressions with Runnable
चूंकि Runnable बिल्कुल एक abstract method वाला एक functional interface है, आप पूरी class के बजाय एक lambda expression का उपयोग करके compact thread declarations लिख सकते हैं। यह सिर्फ एक run() body define करने के लिए anonymous inner class लिखने की formalities हटा देता है।
उदाहरण: Lambda Expressions with Runnable
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) throws InterruptedException {
// Create a new Thread object and store it in t
Thread t = new Thread(() -> System.out.println("Lambda instead of anonymous class"));
// Start the thread; it runs in parallel with this code
t.start();
// Wait for the thread to finish
t.join();
}
}
Login to try C/C++/Java/PHP code in the editor
Thread vs. Runnable
सीधे Thread को extend करने की तुलना में आमतौर पर Runnable implement करने को प्राथमिकता दी जाती है, क्योंकि Java classes सिर्फ एक parent class को extend कर सकती हैं। इसके बजाय एक interface implement करना आपकी class को ज़रूरत पड़ने पर किसी दूसरी class को extend करने के लिए अब भी स्वतंत्र छोड़ देता है, जो Thread को extend करने से संभव न होता।
उदाहरण: Thread vs. Runnable
// Define the class Main
public class Main {
// Define the class Worker
static class Worker extends Thread {
public void run() { System.out.println("Extends Thread -- can't extend anything else"); }
}
static class Job implements Runnable {
public void run() { System.out.println("Implements Runnable -- free to extend another class"); }
}
// Program entry point: the JVM starts running here
public static void main(String[] args) throws InterruptedException {
new Worker().start();
// Create a new thread to run the given task
new Thread(new Job()).start();
}
}
Login to try C/C++/Java/PHP code in the editor
- सीधे
task.run()call करना और यह सोचना कि एक नया thread शुरू हुआ; आपको Runnable कोnew Thread(task).start()में पास करना चाहिए। - यह उम्मीद करना कि
run()एक value return करेगा या एक checked exception फेंकेगा, जबकि यहvoidreturn करता है और checked exceptions declare नहीं कर सकता; उसके लिएCallableका उपयोग करें। - कई threads के बीच एक Runnable object share करना जबकि वह बिना synchronization के एक instance field बदलता है, जिससे race conditions बनती हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: