2

The following code is supposed to take two integers (one thread for each input) from the user, add them (using a 3rd thread) and print the sum. But the program terminates after the first thread.

import java.util.Scanner;

public class T1 {

    public static void main(String args[]) {
        num1 oo = new num1();
        num2 t1 = new num2();

        add s = new add();

        int flag = 0;
        if(oo.flag == 0 && flag == 0) {
            oo.start();
            flag = 1;
        }

        if(oo.flag == 1 && flag == 1)
            t1.start();

        if(oo.flag == 1 && t1.flag2 == 1)
            s.start();

    }

}

class num1 extends Thread //implements Runnable
{
    int n1,flag = 0;
    Scanner obj = new Scanner(System.in);

    public void run() {
        System.out.println("enter first no");
        n1 = obj.nextInt();
        flag = 1;

        System.out.print("In num1 and flag is " + flag + "\n");
    }

}

class num2  extends Thread {
    int n2,flag2 = 0;
    Scanner obj = new Scanner(System.in);

    public void run() {
        System.out.println("enter second no");
        n2 = obj.nextInt();
        flag2 = 1;
        System.out.print("In num2 and flag is " + flag2 + "\n");
    }

}

class add extends Thread {
    public void run() {
        num1 o = new num1();
        num2 t = new num2();

        System.out.print("The sum is " + (o.n1 + t.n2) + "\n");
    }

}

OUTPUT it gives:

enter first no
5
In num1 and flag is 1
Jonny Henly
  • 3,725
  • 4
  • 23
  • 41

2 Answers2

1

Even if you use join() (the correct thing to do) your code will still not function the way you intend it to. Your main thread creates a num1 and a num2 thread, which both prompt the user for input (which brings up another problem mentioned below) then creates an add thread, which itself creates new num1 and num2 threads, which will ask the user for input again. So you will be asking the user for input 4 times, the first 2 times will be redundant.

If this program is meant as a learning exercise then disregard what you've learned from it (other than the need to use join()). You should not collect user input from multiple threads, instead you should collect input on your main thread and pass it to the other threads.

Another issue, apart from this, is that you create multiple Scanner objects wrapping System.in. Never create multiple Scanner objects using System.in, never. More on that from this Stack Overflow question: Java Multiple Scanners

Jonny Henly
  • 3,725
  • 4
  • 23
  • 41
1

There are following problems with your provided code:

  • Multiple instance of System.in is referenced/opened via Scanner class. It creates problem because single object(System.in) is being used in more than one thread.
  • In add.run(), instance of num1 and num2 thread are created but never started.
  • Threads are not joined using join(), so if public static void main(String args[]) is terminated then thread is also terminated.Also thread num1 o and num12 t should be joined before summing up the values o.n1 and t.n2, so that respective vaues can be read properly.
  • In public static void main(String args[]), num1 oo and num2 t1 are created. But its value cannot be used to sum in run() method of class add. So it is logically incorrect.

Following is corrected code(Unused variables are removed for optimization reason):

import java.util.Scanner;

class T1 
{
    public static Scanner obj = new Scanner(System.in);
    public static void main(String args[]) {
        add s = new add();
        try{
            s.start();
            s.join();
        }catch(Exception ex){}
    }
}

class num1 extends Thread {
    int n1;
    public void run() {
        System.out.println("enter first no");
        n1 = T1.obj.nextInt();
        System.out.print("In num1 is " + n1 + "\n");
    }
}

class num2  extends Thread {
    int n2;
    public void run() {
        System.out.println("enter second no");
        n2 = T1.obj.nextInt();
        System.out.print("In num2 is " + n2 + "\n");
    }
}

class add extends Thread {
    public void run() {
        num1 o = new num1();
        num2 t = new num2();
        o.start();
        t.start();
        try {
            o.join();
            t.join();
        }catch(Exception ex){}

        System.out.print("The sum is " + (o.n1 + t.n2) + "\n");
    }
}
cse
  • 3,622
  • 2
  • 17
  • 36