简体   繁体   中英

error while using isALive() or join method

I'm trying to use isALive and join method but its throwing an error like can not find symbol....please tell me where is the error exactly in this program. and what is the use of join method.i know that it is a wait for threads to finish but i want in details.

class newthread1 implements Runnable {
    newthread1() {
        Thread t = new Thread(this, "FirstThread");
        System.out.println("Child Thread:" + t);
        t.start();
    }

    public void run() {
        System.out.println("We are in processing for 1st thread");
        int p = 1000, t = 3;
        double r = 3.5, si;
        try {
            si = (p * r * t) / 100;
            System.out.println("Simple Interest:" + si);
        } catch (ArithmeticException e) {
            System.out.println("Error:" + e);
        }
    }
}

class newthread2 implements Runnable {

    newthread2() {
        Thread t = new Thread(this, "SecondThread");
        System.out.println("Child Thread:" + t);
        t.start();
    }

    public void run() {
        try {
            System.out.println("We are in processing for 2nd thread");
            double a, r = 4.3;
            int p = 1000, n = 3;
            double temp = Math.pow((1 + r / 100), n);
            a = temp * p;
            System.out.println("Compound interest:" + a);
        } catch (ArithmeticException e) {
            System.out.println("Error:" + e);
        }
    }
}

class mainthread {
    public static void main(String args[]) {
        newthread1 t11 = new newthread1();
        new newthread2();
        boolean b = t1.t.isAlive();
        System.out.println("Thread is alive:" + b);
        t1.t.join();
    }
}   

Solution 1
Change your main method as

class mainthread {
    public static void main(final String args[]) throws InterruptedException {
        Thread thread = new Thread(new newthread1());
        newthread1 t11 = new newthread1();
        new newthread2();
        boolean b = thread.isAlive();
        System.out.println("Thread is alive:" + b);
        thread.join();
    }
}

and to run a thread call thread.start() , creating an instnace of an runnable object will not automatically start running. You explicitly tell thread to start or stop.

Solution 2
or you can create Thread object 't' as global varibable and change class as

class newthread1 implements Runnable {
    public Thread t;

    newthread1() {
        t = new Thread(this, "FirstThread");
        System.out.println("Child Thread:" + t);
        t.start();
    }

    @Override
    public void run() {
        System.out.println("We are in processing for 1st thread");
        int p = 1000, t = 3;
        double r = 3.5, si;
        try {
            si = p * r * t / 100;
            System.out.println("Simple Interest:" + si);
        } catch (ArithmeticException e) {
            System.out.println("Error:" + e);
        }
    }

    public Thread getT() {
        return t;
    }
}

and then main method as

class mainthread {
    public static void main(final String args[]) throws InterruptedException {
        newthread1 t11 = new newthread1();
        new newthread2();
        boolean b = t11.t.isAlive();
        System.out.println("Thread is alive:" + b);
        t11.t.join();
    }
}

I personally advice you to first take a tutorial regarding basics of Java. I was convinced that you are not clear about basic Java from the following:

boolean b=t1.t.isAlive();

You don't have a varibale named defined as t1 and still you try to use it.

The compiler won't find any variable named t1 and it will complain Cannot find symbol t1

I think you wanted to use t11 .

Also even if you use t11 it will still complain because you don't have t as a class variable in your class newthread1 , instead you have defined a local variable inside the constructor

Also try to read some java standards like how to declare a class, naming conventions, etc.

It will help you a lot in future.

First error , You have defined the reference variable as t11 :

 newthread1 t11=new newthread1();

Hence use t11 in your code:

boolean b=t11.t.isAlive(); // change t1 to t11

Secondly , there is no Thread t instance variable defined in newthread1 or newthread2 classes. This might help :

class newthread1 implements Runnable{
  Thread t; // make this an instance variable , currently it is local to constructor
  newthread1()
 {
   t=new Thread(this,"FirstThread");
   System.out.println("Child Thread:"+t);
   t.start();
 }

To create a thread in java there are two ways
1.By implementing Runnable interface.
2.By extending Thread class.

.
So that your object will get Thread behavior.

.
So that your object will get Thread behavior.

But you have not followed any of the above two ways,

In your code the statement newthread1 t11 = new newthread1(); creates only simple object
not the Thread object .
But your trying to invoke Thread methods on normal object that leads to compilation errors.

To avoid errors you need to follow any one of above two ways.
More specific you have to
replace newthread1 t11 = new newthread1(); with this statement

 Thread thread = new Thread(new newthread1());//first way implements Runnable

or replace class newthread1 implements Runnable{ with this statement

class newthread1 extends Thread implements Runnable{//second way extends Thread

I'm trying to use isALive

There is no such method. Do you mean isAlive() ?

and join method but its throwing an error like can not find symbol

That's a compile error, probably because of the mis-spelling. Compile errors are printed, not 'thrown'.

please tell me where is the error exactly in this program.

The compiler has already done that, and you haven't posted it here. If you're expecting someone else to recompile your program for you just to find the error site, I suggest you may have a long wait.

and what is the use of join method.i know that it is a wait for threads to finish but i want in details.

The details are to be found in the Javadoc . I could quote it here but frankly I can't see the point when you should already have read it. If there was something there you didn't understand, you should have said so in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM