Program 11,12
Program 11,12
package lab.stack;
try
{
// Ensure the main thread waits for the threads to finish
thread1.join();
thread2.join();
thread3.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
OUTPUT :
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;
// Main method
public static void main(String[] args)
{
// Create an instance of MyThread
MyThread myThread = new MyThread();
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