0% found this document useful (0 votes)
6 views

11th&12th(1)

Uploaded by

dhanyakrish143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

11th&12th(1)

Uploaded by

dhanyakrish143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Program 11: Runnable Interface

Write a program to illustrate creation of threads using runnable class.


(start method start each of
the newly created thread. Inside the run method there is sleep() for
suspend the thread for 50 0
milliseconds).

class MyRunnable implements Runnable {


private volatile boolean running = true;
@O verride
@SuppressWarnings("deprecation")
public void run() {
while (running) {
try {
// Suppress deprecation warning for Thread.sleep()
Thread.sleep(50 0 );
System.out.println("Thread ID: " +
Thread.currentThread().getId() + " is running.");
}
catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
}
}
public void stopThread() {
running = false;
}
}
public class RunnableThreadExample {
public static void main(String[] args) {
// Create five instances of MyRunnable
MyRunnable myRunnable1= new MyRunnable();
MyRunnable myRunnable2 = new MyRunnable();
MyRunnable myRunnable3 = new MyRunnable();
MyRunnable myRunnable4 = new MyRunnable();
MyRunnable myRunnable5 = new MyRunnable();
// Create five threads and associate them with MyRunnable
instances
Thread thread1= new Thread(myRunnable1);
Thread thread2 = new Thread(myRunnable2);
Thread thread3 = new Thread(myRunnable3);
Thread thread4 = new Thread(myRunnable4);
Thread thread5 = new Thread(myRunnable5);
// Start the threads
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
// Sleep for a while to allow the threads to run
try {
Thread.sleep(50 0 );
} catch (InterruptedException e) {
e.printStackTrace();
}
// Stop the threads gracefully
myRunnable1.stopThread();
myRunnable2.stopThread();
myRunnable3.stopThread();
myRunnable4.stopThread();
myRunnable5.stopThread();
}
}

Program 12: Thread Class


Develop a program to create a class MyThread in this class a
constructor, call the base class
constructor, using super and start the thread. The run method of the
class starts after this. It can
be observed that both main thread and created child thread are
executed concurrently.
class MyThread extends Thread {
// Constructor calling base class constructor using super
public MyThread(String name) {
super(name);
start(); // Start the thread in the constructor
}

// The run method that will be executed when the thread starts
@O verride
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " Count:
" + i);
try {
Thread.sleep(50 0 ); // Sleep for 50 0 milliseconds
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "
Thread interrupted.");
}
}
}
}

public class ThreadConcurrentExample {


public static void main(String[] args) {
// Create an instance of MyThread
MyThread myThread = new MyThread("Child Thread");
// Main thread
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " Thread
Count: " + i);
try {
Thread.sleep(50 0 ); // Sleep for 50 0 milliseconds
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + "
Thread interrupted.");
}
}
}
}

You might also like