#why can we pass a class object as a parameter for a thread object?

1 messages · Page 1 of 1 (latest)

mystic cedar
#

Ex.
Lets say there's a Thread1 class which implements runnable where the run method is printing "Hi"
and then you create an object
Thread thread1 = new Thread(new Thread1());
Why is this feature there?

safe tulipBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Ex. Lets say there's a Thread1 class which implements runnable where the run method is printing "Hi" and then you create an object Thread thread1 = new Thread(new Thread1()); Why is this feature there?

hread1 thread1 = new Thread1();

You can pass this object as a parameter when creating a new thread like so:

Thread t = new Thread(thread1);

This is possible because the Thread class in Java has a constructor that can take an object that implements the Runnable interface. When you pass an object that implements Runnable to the Thread constructor, it creates a new thread and calls the run method of the object you passed in.

This allows you to separate the code that defines what the thread does (in the run method of your class) from the code that actually creates and manages the thread. It also makes your code more modular and reusable, as you can easily create multiple threads with different behavior by passing different objects to the Thread constructor.

Overall, passing a class object as a parameter for a thread object in Java allows for more flexibility and better organization of your code when working with threads.

mystic cedar
#

because Thread already impelments runnable

harsh cave
#

Which would be the same as doing this:

var t = new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("abc");
    }
});
#

Or the same as doing this:

private static class MyRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("abc");
    }
}

public static void main(String[] args) {
    var t = new Thread(new MyRunnable());
}
#

Since Thread also implements Runnable, you can also pass it.
Note however, that if you do it, then it won't create two threads, it will still just be a single thread, executing the run method of your Thread1 class.
It never calls Thread1.start, just the Thread1.run method

swift ruin
#

The Thread class is just the mechanism by which you get access to the JVM's ability to create processing threads (or virtual threads).

Historically you might create your own subclass of Thread, or pass a Runnable - most often we'd just pass a Runnable and not subclass Thread itself.

Since Thread implements Runnable, you can pass a reference to a Thread as the Runnable to a new Thread instance... not really useful, but legal.

As Monke says, this isn't actually going to start two processing threads. And if one was already started from the first Thread, this doesn't relate the two in a really meaningful way - except to run the risk of shared state if your first thread is using any instance state in it's run() method that might now also be used by a second thread. Here there be dragons.

mystic cedar
#

like maybe theres like a constructor like

public Thread(Runnable rAble){
this.logic bla bla bla
}

so when the start() method is called it calls the runnable object run() method

swift ruin
harsh cave
# mystic cedar like maybe theres like a constructor like public Thread(Runnable rAble){ ...

What a Thread does is rather simple (except for actually spinning up a thread):

public class Thread implements Runnable {
    private Runnable task;

    public Thread(Runnable run) {
        this.task = run;
    }

    @Override
    public void run() {
        task.run();
    }

    public void start() {
        actually_spin_up_a_thread_and_execute_runnable_code(this.run);
    }

    public void join() {
        wait_for_the_actually_started_thread();
    }
}
```Obviously, as you may have noticed, I've taken some drastic simplifications here, because it's also not **that** simple because there's so called "Platform threads" and "Virtual threads".
If you want to know more about that just read the `Thread` documentation: https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Thread.html
austere imp
#

it's caused many problems. it's there because inheritance used to be a lot more common for changing implementations

swift ruin
#

Indeed. And can't now be taken away without breaking old code.

mystic cedar
#

ohh okay