Understanding java code for a stream

I just looked through some Java code and I came across the following program

public class LengthOfString extends Thread {
    static String s;

    public void run(){
        System.out.println("You Have Enter String: " + s +"  Length Of It is :" + s.length());
    }

    public static void main(String[] args) throws InterruptedException {
        s = "This IS String";
        LengthOfString h = new LengthOfString(); //creating the object of class
        Thread t = new Thread(h);   //why we have passed this object here???
        t.start();
    }
} 

I realized that it is used to print the length of a line, but I have a problem understanding the commented line. Please help me understand why this implementation was used.

+3
source share
2 answers

There are actually 2 ways to create a stream in java .

  • Provide a Runnable object. The Runnable interface defines a single method, run, designed to hold code that runs on a thread. The runnable object is passed to the Thread constructor.

  • . Thread Runnable, . Thread, .

,

new LengthOfString().start();

LengthOfString h=new LengthOfString(); //creating the object of class

Thread t=new Thread(h);   //why we have passed this object here???

 t.start();

Edit:

Thread public Thread(Runnable target), Runnable , thread, run() .

+1

Thread t = new Thread (h), LengthOfString Thread. , , Runnable Interface. Thread Runnable , Runnable Objects

+1

All Articles