Docsity

Java programming threadds, Exercises of Database Programming

Toggle sidebar
bg1
Threads:
A thread in Java is a lightweight, independent unit of execution that
runs within a single process. Threads allow multiple tasks to run
concurrently within a single application, thereby improving the
application's performance and responsiveness.
Sample code:
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.
pf3
pf4

Partial preview of the text

Download Java programming threadds and more Exercises Database Programming in PDF only on Docsity!

Threads:

A thread in Java is a lightweight, independent unit of execution that

runs within a single process. Threads allow multiple tasks to run

concurrently within a single application, thereby improving the

application's performance and responsiveness.

Sample code:

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.

Example 1 : Example of a Java program that uses threads to print the

numbers 1 to 10 in two separate threads:

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.