Home>

java create thread (runnable interface and thread class)

most situation,Create a thread by instantiating a thread object.Java defines two ways:

Implement the runnable interface; Can inherit the thread class.

Each method is described below in turn.

Implementing the runnable interface

The easiest way to create a thread is to create a class that implements the runnable interface.runnable abstracts a unit of code execution.You can create a thread for each object by implementing the runnable interface method.To implement the runnable interface, a class only needs to implement a simple method of run (),The method is declared as follows:

 public void run ()

You can define code in run () to build a new thread.It is important to understand that the run () method can call other methods just like the main thread,Referencing other classes,Declare variables.The only difference is that run () establishes another concurrent thread entry in the program.When run () returns, the thread ends.

After you have created a class that implements the runnable interface,You need to instantiate an object of the thread class inside the class.The thread class defines several constructors.We will use the following:

 thread (runnable threadob, string threadname)

In the constructor,threadob is an instance of a class that implements the runnable interface.This defines the starting point for thread execution.The name of the new thread is defined by threadname.

After creating a new thread,It does not run until its start () method is called, which is defined in the thread class.Essentially, start () executes a call to run (). The start () method is declared as follows:

 void start ()

The following example creates a new thread and starts it to run:

//create a second thread.
class newthread implements runnable {
  thread t;
  newthread () {
    //create a new, second thread
    t=new thread (this, "demo thread");
    system.out.println ("child thread:" + t);
    t.start ();//start the thread
  }
  //this is the entry point for the second thread.
  public void run () {
    try {
      for (int i=5;i>0;i--) {
        system.out.println ("child thread:" + i);
        thread.sleep (500);
      }
    } catch (interruptedexception e) {
      system.out.println ("child interrupted.");
    }
    system.out.println ("exiting child thread.");
  }
}
class threaddemo {
  public static void main (string args []) {
    new newthread ();//create a new thread
    try {
      for (int i=5;i>0;i--) {
        system.out.println ("main thread:" + i);
        thread.sleep (1000);
      }
    } catch (interruptedexception e) {
      system.out.println ("main thread interrupted.");
    }
    system.out.println ("main thread exiting.");
  }
}

In the newthread constructor,The new thread object is created by the following statement ::

 t=new thread (this, "demo thread");

The previous statement this indicates that in this object i want the new thread to call the run () method. Then, start () is called, starting with the run () method to start the execution of the thread.This causes the child thread's for loop to begin execution.After calling start (), the constructor of newthread returns to main (). When the main thread is resumed,It reaches the for loop. Two threads continue to run,Share CPUs until their loop ends.The output of this program is as follows:

child thread:thread [demo thread, 5, main]
main thread:5
child thread:5
child thread:4
main thread:4
child thread:3
child thread:2
main thread:3
child thread:1
exiting child thread.
main thread:2
main thread:1
main thread exiting.

As mentioned earlier,In a multithreaded program,Usually the main thread must be the last thread to finish running.In fact, some old jvms, if the main thread ends before the child thread,Java's runtime system may "hang". The preceding program guarantees the end of the main thread,Because the main thread sleeps for 1000 milliseconds, and the child thread is only 500 milliseconds. This causes the child thread to finish before the main thread ends.in short,You will see a better way to wait for the thread to end.

Extended thread

Another way to create threads is to create a new class that extends the thread class and then create an instance of that class.When a class inherits thread, it must override the run () method, which is the entry point for the new thread.It must also call the start () method to start new thread execution.Let's rewrite the previous program with the extended thread class:

//create a second thread by extending thread
class newthread extends thread {
  newthread () {
    //create a new, second thread
    super ("demo thread");
    system.out.println ("child thread:" + this);
    start ();//start the thread
  }
  //this is the entry point for the second thread.
  public void run () {
    try {
      for (int i=5;i>0;i--) {
        system.out.println ("child thread:" + i);
        thread.sleep (500);
      }
    } catch (interruptedexception e) {
      system.out.println ("child interrupted.");
    }
    system.out.println ("exiting child thread.");
  }
}
class extendthread {
  public static void main (string args []) {
    new newthread ();//create a new thread
    try {
      for (int i=5;i>0;i--) {
        system.out.println ("main thread:" + i);
        thread.sleep (1000);
      }
    } catch (interruptedexception e) {
      system.out.println ("main thread interrupted.");
    }
    system.out.println ("main thread exiting.");
  }
}

This program produces the same output as the previous version.Child threads are generated by instantiating newthread objects,This object is derived from the thread class. Note the call to super () in newthread. This method calls the following form of the thread constructor:

 public thread (string threadname)

Here, threadname specifies the thread name.

Choose the right method

At this point, you must be wondering why Java has two ways to create child threads.Which one is better?All problems come down to one point.The thread class defines multiple methods that can be overloaded by derived classes.For all methods,The only thing that must be overloaded is the run () method. This is of course the same method required to implement the runnable interface.Many java programmers think that classes should only be extended when they are enhanced or modified.So if you don't overload other methods of thread,It is best to implement only the runnable interface. It's up to you.However, elsewhere in this chapter,We apply classes that implement the runnable interface to create threads.

  • Previous pacejs page loading progress bar plugin
  • Next DropDownList adds client drop event action