Knowee
Questions
Features
Study Tools

Develop a multithreaded Java program with two threads. One threadgenerates even numbers from 1 to 100 and another thread generates oddnumbers from 1 to 100. Ensure that the threads do not interfere.

Question

Develop a multithreaded Java program with two threads. One threadgenerates even numbers from 1 to 100 and another thread generates oddnumbers from 1 to 100. Ensure that the threads do not interfere.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple Java program that uses two threads to generate even and odd numbers from 1 to 100:

public class Main {
    public static void main(String[] args) {
        Thread evenThread = new Thread(new NumberGenerator(2, 100, 0), "Even");
        Thread oddThread = new Thread(new NumberGenerator(2, 100, 1), "Odd");

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

class NumberGenerator implements Runnable {
    private final int start;
    private final int end;
    private final int offset;

    NumberGenerator(int start, int end, int offset) {
        this.start = start;
        this.end = end;
        this.offset = offset;
    }

    @Override
    public void run() {
        for (int i = this.start; i <= this.end; i++) {
            if (i % 2 == this.offset) {
                System.out.println(Thread.currentThread().getName() + ": " + i);
            }
        }
    }
}

In this program, we have a NumberGenerator class that implements Runnable. This class generates numbers from start to end that are either even or odd, depending on the offset (0 for even, 1 for odd).

In the main method, we create two threads: evenThread and oddThread. evenThread generates even numbers from 2 to 100, and oddThread generates odd numbers from 1 to 100. We start both threads with the start method.

The threads do not interfere with each other because they do not share any mutable state. Each thread has its own NumberGenerator instance with its own start, end, and offset values.

This problem has been solved

Similar Questions

(This is from Programming Exercise P4.1 at the end of Chapter 4 of Java for Everyone.) Write program fragments (i.e., you do not need to write complete programs) with loops that compute:The sum of all even numbers between 2 and 100 (inclusive).The sum of all squares between 1 and 100 (inclusive).All powers of 2 from 20 up to 220 (inclusive).The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b.The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)

Create a program in Java that will accept two integers. If the sum of the integers is odd, display the higher integer, else, display the lower integer.

You have been provided with the following loop. It is required to do some processing on all the odd numbers between one and maxNumber inclusive.int index = 1;do{    //Some processing code is done here    //insert line here} while (index < maxNumber);What line of code can be inserted before the end of the loop, so that the loop only checks the odd numbers between 1 and maxNumber? Multiple responses are allowed — select all that apply.

public class Hello{ public static void main(String args[]) { int num = 190; String rev_num = " " ; while (num!=0){ int digit = num%10; rev_num = rev_num+digit; num = num /= 10 ; } System.out.println((Integer.parseInt(rev_num)));} } Exception in thread "main" java.lang.NumberFormatException: For input string: " 091" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:654) at java.base/java.lang.Integer.parseInt(Integer.java:786)

answerWhat will be the output of the following Java program?class output_odd   {    public static void main(String args[])     {            int p = 2;       int q = 0;       for ( ; q < 8; ++q)        {         if (q % p == 0)            continue;           else if (q == 6)           break;         else          System.out.print(q + " ");       }    }   }

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.