← Back to Advanced Java Course | Chapter 2: Multithreading | Lesson 3 of 8

Java का Runnable Interface

Runnable एक job description है जो कहता है 'यहां करने के लिए काम है', जिसे फिर एक thread कर सकता है। यह किसी task को card पर लिखकर एक helper को सौंपने जैसा है।
Syntax
java
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?

java
// 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();
	}
}

Implementing Runnable

Runnable implement करने के लिए, अपनी class पर implements keyword का उपयोग करें और इसके run() method के लिए एक body प्रदान करें। यह 'क्या काम होना चाहिए' को 'यह कैसे execute होता है' से अलग करता है, क्योंकि एक ही Runnable object को किसी Thread, किसी ExecutorService, या किसी भी चीज़ को सौंपा जा सकता है जो इसे चलाना जानती है।

उदाहरण: Implementing Runnable

java
// 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();
	}
}

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

java
// 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();
	}
}

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

java
// 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();
	}
}

Thread vs. Runnable

सीधे Thread को extend करने की तुलना में आमतौर पर Runnable implement करने को प्राथमिकता दी जाती है, क्योंकि Java classes सिर्फ एक parent class को extend कर सकती हैं। इसके बजाय एक interface implement करना आपकी class को ज़रूरत पड़ने पर किसी दूसरी class को extend करने के लिए अब भी स्वतंत्र छोड़ देता है, जो Thread को extend करने से संभव न होता।

उदाहरण: Thread vs. Runnable

java
// 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();
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. सीधे task.run() call करना और यह सोचना कि एक नया thread शुरू हुआ; आपको Runnable को new Thread(task).start() में पास करना चाहिए।
  2. यह उम्मीद करना कि run() एक value return करेगा या एक checked exception फेंकेगा, जबकि यह void return करता है और checked exceptions declare नहीं कर सकता; उसके लिए Callable का उपयोग करें।
  3. कई threads के बीच एक Runnable object share करना जबकि वह बिना synchronization के एक instance field बदलता है, जिससे race conditions बनती हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.