Program 12
Program 12
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.
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.
9. Creates a checknegative thread instance, passing the thread name "Second thread"
and the num array as parameters.
11. Catches any InterruptedException that might occur during thread execution.
Program flow
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.
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.
Share
New Chat
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).
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.
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.