Page 157 - CITS - CSA - TP (Volume 2) - Hindi
P. 157
कं ूटर सॉ वेयर ए ीके शन - CITS
अ ास 103 : Runnable इंटरफ़े स को काया त करके ेड बनाएँ (Create thread by implementing
Runnable interface)
उ े
इस अ ास के अंत म आप यह कर सक गे
• JAVA म ेड्स के िनमा ण और िन ादन को जानना
• सुपर ास और सब ास का उपयोग करके JAVA ो ाम डेवलप करना।
आव कताएं (Requirements)
उपकरण/साम ी (Tools/Materials)
• िवंडोज OS वाला PC/लैपटॉप
• SDK सॉ वेयर
• टे एिडटर (िवजुअल ू िडयो/सबलाइम/नोटपैड)
ि या (Procedure)
class MyRunnable implements Runnable {
private String message;
public MyRunnable(String message) {
this.message = message;
}
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + “: “ + message + “ “ + i);
}
}
}
public class ThreadExample {
public static void main(String args[]) {
// Create instances of MyRunnable with different messages
MyRunnable myRunnable1 = new MyRunnable(“Thread 1”);
MyRunnable myRunnable2 = new MyRunnable(“Thread 2”);
// Create Thread objects using MyRunnable instances
Thread t1 = new Thread(myRunnable1, “Thread A”);
Thread t2 = new Thread(myRunnable2, “Thread B”);
// Start the threads
t1.start();
t2.start();
}
}
143

