Program:11. 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 500 milliseconds).
Aim: Exception handling in java, introduction to throwable class, throw, finally.
package lab.stack;
public class Thread_Example implements Runnable
{
private String name;
// Constructor to initialize the thread name
public Thread_Example(String name)
{
this.name = name;
}
public void run()
{
System.out.println("Thread started: " + name);
try
{
// Simulate work by sleeping for 500 milliseconds
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread ended: " + name);
}
public static void main(String[] args)
{
// Create Runnable objects with different names
Runnable runnableExample1 = new Thread_Example("Thread-1");
Runnable runnableExample2 = new Thread_Example("Thread-2");
Runnable runnableExample3 = new Thread_Example("Thread-3");
// Create Thread objects using the Runnable instances
Thread thread1 = new Thread(runnableExample1);
Thread thread2 = new Thread(runnableExample2);
Thread thread3 = new Thread(runnableExample3);
// Start the threads
thread1.start();
thread2.start();
thread3.start();
try
{
// Ensure the main thread waits for the threads to finish
thread1.join();
thread2.join();
thread3.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
// Optionally, print a message after all threads are done
System.out.println("All threads have completed execution.");
}
}
OUTPUT :
Thread started: Thread-3
Thread started: Thread-2
Thread started: Thread-1
Thread ended: Thread-2
Thread ended: Thread-3
Thread ended: Thread-1
All threads have completed execution.
Program:12. 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.
Aim: Demonstrate the file operations in java programming.
package lab.stack;
public class MyThread extends Thread
{
// Constructor
public MyThread()
{
super(); // Calls the constructor of the Thread class
System.out.println("Child Thread created.");
}
// Override the run() method
@Override
public void run()
{
try
{
for (int i = 0; i < 5; i++)
{
System.out.println("Child Thread: " + i);
Thread.sleep(500); // Sleep for 500 ms
}
}
catch (InterruptedException e)
{
e.printStackTrace(); // Handle the exception if the thread is interrupted
}
}
// Main method
public static void main(String[] args)
{
// Create an instance of MyThread
MyThread myThread = new MyThread();
// Start the child thread
myThread.start();
try
{
// Main thread processing
for (int i = 0; i < 5; i++)
{
System.out.println("Main Thread: " + i);
Thread.sleep(500); // Sleep for 500 ms
}
// Wait for the child thread to complete before continuing the main thread
myThread.join(); // Ensures main thread waits for myThread to finish
}
catch (InterruptedException e)
{
e.printStackTrace(); // Handle interruption exception
}
}
}
Output:
Child Thread created.
Main Thread: 0
Child Thread: 0
Main Thread: 1
Child Thread: 1
Main Thread: 2
Child Thread: 2
Main Thread: 3
Child Thread: 3
Main Thread: 4
Child Thread: 4