Write A Program That Executes Two Threads. One Thread Will Print The Even Numbers And Another Thread Will Print Odd Numbers From 1 To 200

0

Write A Program That Executes Two Threads. One Thread Will Print The Even Numbers And Another Thread Will Print Odd Numbers From 1 To 200

Write a program that executes two threads. One thread will print the even numbers and another thread will print odd numbers from 1 to 200

Before demonstrate the program we will understand the concept of threads.


In Java, a thread is a lightweight unit of execution that can run concurrently with other threads within the same program. Each thread has its own call stack and program counter, which allows it to execute independently of other threads.


Threads can be used to perform tasks in parallel, which can improve the performance and responsiveness of your program. For example, you might use threads to perform long-running calculations or to handle user input while other tasks are being performed.


There are two ways to create a thread in Java: by extending the `Thread` class or by implementing the `Runnable` interface.


To create a thread by extending the `Thread` class, you define a new class that extends `Thread` and overrides its `run()` method. This method contains the code that will be executed when the thread is started. Here's an example:


class MyThread extends Thread {
    public void run() {
        // Code to be executed in this thread
    }
}


To start this thread, you create an instance of your custom `MyThread` class and call its `start()` method:


MyThread myThread = new MyThread();
myThread.start();


Alternatively, you can create a thread by implementing the `Runnable` interface. This involves defining a class that implements `Runnable` and provides an implementation for its `run()` method. Here's an example:


class MyRunnable implements Runnable {
    public void run() {
        // Code to be executed in this thread
    }
}


To start this thread, you create an instance of your custom `MyRunnable` class and pass it as an argument to the constructor of the `Thread` class. You then call the `start()` method on the resulting `Thread` object:


MyRunnable myRunnable = new MyRunnable();
Thread myThread = new Thread(myRunnable);
myThread.start();

Here's an example program that creates two threads, one that prints the even numbers and another that prints the odd numbers from 1 to 200:

Program
class EvenThread extends Thread {
    public void run() {
        for (int i = 2; i <= 200; i += 2) {
            System.out.println(i);
        }
    }
}

class OddThread extends Thread {
    public void run() {
        for (int i = 1; i <= 200; i += 2) {
            System.out.println(i);
        }
    }
}

public class ThreadExample {
    public static void main(String[] args) {
        EvenThread evenThread = new EvenThread();
        OddThread oddThread = new OddThread();

        evenThread.start();
        oddThread.start();
    }
}

Output
2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15 18 17 20 19 22 21 24 23 26 25 28 27 30 29 32 31 34 33 36 35 38 37 40 39 42 41 44 43 46 45 48 47 50 49 52 51 54 53 56 55 58 57 60 59 62 61 64 63 66 65 68 67 70 69 72 71 74 73 76 75 78 77 80 79 82 81 84 83 86 85 88 87 90 89 92 91 94 93 96 95 98 97 100 99 102 101 104 103 106 105 108 107 110 109 112 111 114 113 116 115 118 117 120 119 122 121 124 123 126 125 128 127 130 129 132 131 134 133 136 135 138 137 140 139 142 141 144 143 146 145 148 147 150 149 152 151 154 153 156 155 158 157 160 159 162 161 164 163 166 165 168 167 170 169 172 171 174 173 176 175 178 177 180 179 182 181 184 183 186 185 188 187 190 189 192 191 194 193 196 195 198 197 200 199


Explanation
In this example, we create two classes, EvenThread and OddThread, that extend the Thread class. In each class, we define a run() method that prints either the even or odd numbers in the range from 1 to 200. We use a for loop to iterate through the numbers, incrementing by 2 for even numbers and by 1 for odd numbers. Inside the loop, we print the current number using the System.out.println() method.

In the ThreadExample class, we create instances of EvenThread and OddThread and then start them by calling their start() methods. This will cause each thread to run its run() method independently and concurrently, printing the even and odd numbers, respectively, from 1 to 200.

❤️ I Hope This Helps You Understand, Click Here To Do More Exercise In Java Programming.

Post a Comment

0Comments
Post a Comment (0)