YoMama YoMama - 7 days ago 6
Java Question

(Java Multi threading) How to pass parameter in multithreading?

I am getting an error( in last 5 lines) when i pass a parameter in the start() method. The error says: method start() in type thread is not applicable for argument (int). What is the right way to do it?

This program is about printing odd/even numbers using multi-threading in Java.

import java.util.Scanner;

class odd extends Thread
{
public void run(int y){
System.out.println("Odd numbers are:");
for (int i=0;i<=y;i++)
{
if (i/2!=0)
{
System.out.println(i);
}
}
}
}

class even extends Thread{

public void run(int x)
{
System.out.println("Even Numbers are");
for (int i=0; i<=x; i++)
{
if (i/2==0)
{
System.out.println(i);
}
}
}

}


class Star{
public static void main(String[] args)
{
System.out.println("Enter No. upto which even-odd should be printed");
Scanner var=new Scanner(System.in);
int vary= var.nextInt();

odd money=new odd();
even honey=new even();

money.start(vary);

Thread.sleep(5000);
honey.start(vary);

}
}

Answer

There are syntactical and logical errors in your program:

I have corrected your code. Below code is the correct code for printing odd/even numbers using multi-threading in Java.

class jo extends Thread {
    private int number;

    jo(int number) {
        this.number = number;
    }

    @Override
    public void run() {
        System.out.println("Odd numbers are:");
        for (int i = 0; i <= number; i++) {
            if (i % 2 != 0) {
                System.out.println(i);
            }
        }
    }
}

class yo extends Thread {

    private int number;

    yo(int number) {
        this.number = number;
    }

    @Override
    public void run() {
        System.out.println("Even Numbers are");
        for (int i = 0; i <= number; i++) {
            if (i % 2 == 0) {
                System.out.println(i);
            }
        }
    }

}
import java.util.Scanner;

class Star {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("Enter No. upto which eve-od should be printed");
        Scanner var = new Scanner(System.in);
        int vary = var.nextInt();
        jo money = new jo(vary);
        yo honey = new yo(vary);

        money.start();
        Thread.sleep(5000);
        honey.start();

    }

}

Corrections done:

  1. In class Star, there was a syntax error when you were creating object of class jo

2.To achieve multithreading, you need to override run method of thread class. The correct signature of run method is public void run() { //logical code will go here } run method does not accept any input arguments.

3.To start execution of thread you need to invoke the start method of Thread class. It looks like public void start(). start method does not accept any input arguments.

4.I have done the logical correction in your code in jo and yo class. use % operator to check a given number is even or odd. % operator returns remainder, if a remainder comes zero when a number is divided by 2, it means it is a even number else it is odd.

You can run shared code to check the outputs.

Happy coding!

Comments