


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



In this code, the MyThread class extends the Thread class and overrides the run() method. The run() method contains the code that will be executed when the thread is started. The Main class contains the main() method, which is the entry point of the application. In the main() method, an instance of the MyThread class is created and assigned to a variable myThread. Then, the start() method is called on the myThread object to start the thread. When the code is executed, the run() method of the MyThread class will be executed in a separate thread, concurrently with the main() method. This will result in the output "Thread is running." being printed to the console.
In this code, the MyThread class extends the Thread class and overrides the run() method. The run() method contains a loop that iterates 10 times, printing the current iteration number and sleeping for 100 milliseconds after each iteration. In the main() method of the Main class, an instance of the MyThread class is created and started. Then, the main() method also contains a loop that iterates 10 times, printing the current iteration number and sleeping for 100 milliseconds after each iteration. When you run this code, you'll see the output from both the MyThread instance and the main() method interleaved. Each iteration of both loops will take approximately 100
The MyRunnableThread class implements the Runnable interface and overrides the run() method. The run() method contains a loop that iterates 5 times, printing the current iteration number, and sleeping for 100 milliseconds after each iteration. In the main() method of the SingleThread class, an instance of the MyRunnableThread class is created and passed to a new Thread object. The start() method of the Thread object is then called to start the thread. The main() method also contains a loop that iterates 5 times, printing the current iteration number, and sleeping for 100 milliseconds after each iteration. When you run this code, you'll see the output from both the MyRunnableThread instance and the main() method interleaved. Each iteration of both loops will take approximately 100 milliseconds to complete, and since the two threads are running concurrently, you'll see the numbers from 1 to 5 being printed by both threads in an interleaved manner.