Java Thread Methods

Java Thread & Methods 

Java is a language which offers Multithreading as in which we can run threads simultaneously using start() and run() methods. well along side that java provide us with various thread methods to manipulate the thread according to the developer need for instance sleep() or yield() method.

let start with the basic start() and run() method and understand how to create and run a thread in Java.

class one extends Thread {
    @Override
    public void run() {

 for (int i = 0; i < 5; i++) {
 System.out.println("first class is running ");
        }
    }
}

class two extends Thread {
    @Override
    public void run() {

for (int j = 0; j < 5; j++) {
System.out.println("second class is running");
        }
    }
}

class multhreading {
    public static void main(String[] args) {

        one t1 = new one();
        two t2 = new two();
        t1.start();
        t2.start();
    }
}

so as we can see in this code we can see the basic demonstration of the multithreading by extending Thread class. 

As we can see the creation of any Thread start with a class, extending Thread super class from java library. We can also do the same by extending Runnable class but for now we will try to understand by extending Thread class. The very next line we can see @Override annotation its a optional keyword used in java to depict the method is being overridden. The run() method here is the method that is providing us with the runnable interface for our thread.

lets understand the run method in detail,

  • Method Detail

    • run

      void run()
      When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.

      The general contract of the method run is that it may take any action whatsoever.

      See Also:
      Thread.run()

inside the run() method we are using a basic for loop so the we can print the line "first class is running" 5 times also we have done the same for the second class below named as class two similar to class one we are printing the line "second class is running" 5 times using for loop.

in the third class named multithreading we can see the objects for class one and two has been created t1 and t2 using these object we can call both the class but that will not what we are seeking rather we want to run both the class as a thread and to do that we have to use the start() method by using the start method we can run any thread.

let see start() method in detail

  • start

    public void start()
    Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

    The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

    It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

Finally the output for the source code mentioned above






here we can see the behavior of the thread it run on however the OS (Operating System) allocate time or priority to run to each method we can also set our own priority using setPriority() method lets see that in next module.

Comments