We override the run method when the child class is extended from the parent class of the thread.

When we add a subclass stream, do we override its launch method? We know that the Thread class itself implements Runnable, but there is no body for the run method defined in the Runnable class.

This is my photo:

Runnable - the parent class - it has a start method with an empty body.

Thread-child

classA extends Thread-Child of Child,

when we define the run () method in "classA", do we override the run method declared in the Runnable class? Thank you for your time.

+3
source share
2 answers

: Thread Runnable.

Thread run() :

public class HelloThread extends Thread {
    @Override
    public void run() {
        System.out.println("Hello from a thread!");
    }
}

public class Main { 
    // main method just starts the thread 
    public static void main(String args[]) {
        (new HelloThread()).start();
    }
}

Thread , Runnable:

public class HelloRunnable implements Runnable {
    @Override
    public void run() {
        System.out.println("Hello from a thread!");
    }
}

public class Main {
    // notice that we create a new Thread and pass it our custom Runnable
    public static void main(String args[]) {
        (new Thread(new HelloRunnable())).start();
    }
}

, Runnable , , . , , Runnable , :

public class Main {
    public static void main(String args[]) {
        int poolSize = 5;
        ExecutorService pool = Executors.newFixedThreadPool(poolSize);
        pool.execute(new HelloRunnable());
    }
 }

:

+8

Thread, .

, , funcationality.In , buisness , .

, .

0

Source: https://habr.com/ru/post/1774441/


All Articles