PHcoDer
PHcoDer

Reputation: 1236

Calling a method of a class which extends Thread, from another class

I know this is a bit naive question but I want to understand the basic working principle behind multi-threading in java. Consider the following code and say A is executed in Main thread and it starts execution of another worker thread ,defined in class B. I want to know that can B.func1 called from A and run method of B, be executed in parallel or not?

public class A {
    public static void main(String[] args) {
        B obj = new B();
        obj.start();
        obj.func1();
    }
}

public class B extends Thread {
    public B() {
        //constructor
    }
    public void run() {
        while(true) {
            //do somethings
        }
    }
    public void func1() {
        //do someotherthings
    }
}

Upvotes: 5

Views: 8378

Answers (3)

Solomon Slow
Solomon Slow

Reputation: 27115

It's important to understand the difference between a thread and a Thread.

A thread is an independent execution of your code. Often when we talk about how some method or another works we say things like, "It tests the variable x, and if x is less than zero it calls the foobar method..."

Ok, but what is the "it" in that sentence? It is not the method. Methods don't do anything. A method is just a list of instructions, like the list of chores that somebody left for their housemate to perform. The list doesn't do the chores, it's the housemate that does the work (or so we might hope).

The "it" is a thread. Threads are entities in the operating system that execute methods (i.e., they do the chores).

A Thread, on the other hand, is a Java object that your program can use to create and manage new threads. Your program creates a new Thread object by doing:

thread t = new Thread(...);

[Oops! See what I just did? It's not your program, that does the work, it's your program's main thread, or maybe some other thread in your program. It's an easy thing to forget!]

Anyway, it subsequently creates the new thread by calling t.start();


Once you understand all that, then Sergey Tachenov's answer becomes obvious: Calling the methods of a Thread object really is no different from calling methods of any other kind of object.

Upvotes: 2

Ravindra babu
Ravindra babu

Reputation: 38910

There are multiple issues with your code. I have corrected them and added one more statement to print Thread Name in func1().

Working code:

public class A {
    public static void main(String args[]){
        B obj = new B();
        obj.start();
        obj.func1();
    }
}

class B extends Thread{
    public B (){
        //constructor
    }
    public void run(){
        while(true){
            //do somethings
        }
    }
    public void func1 (){
        //do someotherthings
        System.out.println("Thread name="+Thread.currentThread().getName());
    }
}

output:

Thread name=main

Since you are directly calling func1() from main method (A.java) , you will get Thread name = main in output.

If you add same print statement run() method, you will get output as : Thread name=Thread-0

Upvotes: 1

Sergei Tachenov
Sergei Tachenov

Reputation: 24869

There is no magic behind a method call. If you call method from a thread, it is called in exactly the same thread. So since obj.func1() is called from main, it will be run in the main thread. It doesn't matter which class it belongs to or whether or not it extends Thread.

The new thread starts by executing run. Everything called from run and so on will be executed in parallel to main.

Upvotes: 7

Related Questions