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

Program 12

The Java program demonstrates multithreading by creating two classes, checkpositive and checknegative, which extend the Thread class to print positive and negative numbers from an array, respectively. The main class, multithread, manages the thread execution by prompting the user for input, starting the threads, and using join() to wait for their completion. The program showcases concurrent execution, allowing both tasks to run simultaneously, improving performance and responsiveness.

Uploaded by

Umar Yousuff
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program 12

The Java program demonstrates multithreading by creating two classes, checkpositive and checknegative, which extend the Thread class to print positive and negative numbers from an array, respectively. The main class, multithread, manages the thread execution by prompting the user for input, starting the threads, and using join() to wait for their completion. The program showcases concurrent execution, allowing both tasks to run simultaneously, improving performance and responsiveness.

Uploaded by

Umar Yousuff
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

This Java program demonstrates the use of multithreading to print positive and negative numbers

from an array. Here's a breakdown of the code:

Class checkpositive

 This class extends the Thread class, which means it's a thread that can run concurrently with
other threads.

 It has a constructor that takes a String thread name and an int[] array as parameters.

 The constructor calls the super constructor to set the thread name and initializes
the num array.

 The run() method is where the thread's execution begins. It prints "Printing positive values:"
and then iterates over the num array, printing only the positive values (i.e., values greater
than or equal to 0).

Class checknegative

 This class is similar to checkpositive, but it prints negative values (i.e., values less than 0)
instead.

Class multithread

 This is the main class that creates and manages the threads.

 The main() method:

1. Creates a BufferedReader to read input from the user.

2. Creates an int[] array to store 5 integers.

3. Prompts the user to enter 5 array elements and stores them in the num array.

4. Creates a checkpositive thread instance, passing the thread name "First thread" and
the num array as parameters.

5. Calls start() on the thread to begin its execution.

6. Prints whether the thread is alive using isAlive().

7. Calls join() on the thread to wait for it to finish execution.

8. Prints whether the thread is alive again using isAlive().

9. Creates a checknegative thread instance, passing the thread name "Second thread"
and the num array as parameters.

10. Prints whether the second thread is alive using isAlive().

11. Catches any InterruptedException that might occur during thread execution.

12. Prints a message indicating that the main thread is exiting.

Program flow

1. The user is prompted to enter 5 array elements.

2. The checkpositive thread is created and started, which prints positive values from the array.
3. The main thread waits for the checkpositive thread to finish using join().

4. The checknegative thread is created and started, which prints negative values from the array.

5. The main thread prints a message indicating that it's exiting.

Note that the order of execution is not guaranteed, as the threads run concurrently. The output may
vary depending on the order in which the threads execute.

Follow This Topic

Share

New Chat

how does the above code implement multithreading

The above code implements multithreading in the following ways:

1. Extending the Thread class: The checkpositive and checknegative classes extend
the Thread class, which means they are threads that can run concurrently with other
threads.

2. Overriding the run() method: The run() method is where the thread's execution begins. In
this code, the run() method is overridden in both checkpositive and checknegative classes to
perform specific tasks (printing positive and negative numbers, respectively).

3. Creating thread instances: In the main() method, instances


of checkpositive and checknegative threads are created, passing the thread name and
the num array as parameters.

4. Starting the threads: The start() method is called on each thread instance to begin its
execution. This causes the thread to execute its run() method concurrently with other
threads.

5. Using join() method: The join() method is used to wait for a thread to finish execution. In
this code, the main thread waits for the checkpositive thread to finish using t.join() before
creating and starting the checknegative thread.

Here's a step-by-step explanation of how multithreading works in this code:

1. The main thread creates a checkpositive thread instance and starts it using t.start().

2. The checkpositive thread begins executing its run() method, printing positive numbers from
the array.

3. The main thread continues executing, printing whether the checkpositive thread is alive
using t.isAlive().

4. The main thread waits for the checkpositive thread to finish using t.join().
5. Once the checkpositive thread finishes, the main thread creates a checknegative thread
instance and starts it using t1.start().

6. The checknegative thread begins executing its run() method, printing negative numbers from
the array.

7. The main thread continues executing, printing whether the checknegative thread is alive
using t.isAlive().

By using multithreading, the code can perform two tasks concurrently: printing positive numbers and
printing negative numbers. This can improve the overall performance and responsiveness of the
program, especially in scenarios where the tasks are computationally intensive or involve I/O
operations.

You might also like