YoMama
YoMama

Reputation: 41

(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);

}
}

Upvotes: 0

Views: 2480

Answers (3)

Subhra Jyoti Lahiri
Subhra Jyoti Lahiri

Reputation: 320

There are a lot of mistakes in the program. I am pointing them out.

  1. Typos for - jo=money will be jo money = new jo(); money.stary(vary); will be money.start();

  2. Logic is wrong : To check for a number to be even or odd we must check the remainder of division operation by 2 to be 0 or not. Instead you checked the quotient. Assume 4, So 4/2 = 2, and !=0 so the code will consider it as a odd number.

    Now, if it is a%b == 0, it will work like this, 4%2 = 0, thus even.

  3. Thread.start() does not take any parameter, so you have to create a constructor of the class to pass in the limits.

I have modified the code like this:

import java.util.Scanner;

class jo extends Thread
{
    private int limit;
    
    public jo(int limit)
    {
        this.limit = limit;
    }
    
    public void run()
    {
        System.out.println("Odd numbers are");
        
        for (int i = 0 ; i <= limit ; i++)
        {
            if (i%2 != 0)
            {
                System.out.println(i);
            }
        }
    }
}

class yo extends Thread
{
    private int limit;
    
    public yo(int limit)
    {
        this.limit = limit;
    }
    
    public void run()
    {
        System.out.println("Even Numbers are");
        
        for (int i = 0 ; i <= limit ; i++)
        {
            if (i%2 == 0)
            {
                System.out.println(i);
            }
        }
     }
 }


class TestProject
{
    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(5vary);

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

    }
}

I hope this will help you out.

P.S. : Please check your naming conventions. It will be a really difficult for others to understand.

Upvotes: 1

Reena Upadhyay
Reena Upadhyay

Reputation: 2017

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!

Upvotes: 2

Jim Garrison
Jim Garrison

Reputation: 86754

The standard way to do this is to

  1. declare one or more member variables in the thread class to hold parameters
  2. declare a constructor in the thread class to receive and store these parameters
  3. pass the parameters when instantiating the thread class

For your example:

class Jo extends Thread
{
    int var;
    public Jo(int var) {
        this.var = var;
    }
    public void run(int y) {
        // some code using this.var
        ...
    }
}

...

Jo money = new Jo(vary);
money.start();

Upvotes: 4

Related Questions