Java threads are methods that create threads using the Thread class

  • 2020-04-01 01:49:16
  • OfStack

There are two ways to create threads in Java: using the Thread class and using the Runnable interface. A Thread instance needs to be set up when using the Runnable interface. Therefore, whether a Thread is created through the Thread class or the Runnable interface, an instance of the Thread class or its subclasses must be created. The constructor of the Thread class has been overloaded eight times, as follows:


public Thread( );
 public Thread(Runnable target);
 public Thread(String name);
 public Thread(Runnable target, String name);
 public Thread(ThreadGroup group, Runnable target);
 public Thread(ThreadGroup group, String name);
 public Thread(ThreadGroup group, Runnable target, String name);
 public Thread(ThreadGroup group, Runnable target, String name, long stackSize);

A Runnable target

      An instance of a class that implements the Runnable interface. Note that the Thread class also implements the Runnable interface, so an instance of a class inherited from the Thread class can also be passed into the constructor as a target.

      String name

      The name of the thread. This name can be set by the setName method of the Thread class after the Thread instance is established. If the Thread's name is not set, the Thread USES the default Thread name: thread-n, where N is the order in which the Thread is created and is a positive integer that does not repeat.

      ThreadGroup group

      The group of threads to which the currently created thread belongs. If you do not specify a thread group, all threads are added to a default thread group. The details of thread groups are discussed in detail in a later section.

      Long stackSize

      The size of the thread stack, which is usually an integer multiple of the CPU page. For example, the x86 page size is 4KB. On the x86 platform, the default thread stack size is 12KB.

      A normal Java class can become a Thread class simply by inheriting from the Thread class. You can also execute the Thread code through the start method of the Thread class. Although subclasses of the Thread class can be instantiated directly, the Thread class's run method must be overridden in the subclass to actually run the Thread's code. The following code gives an example of creating a Thread using the Thread class:


package mythread;

   public class Thread1 extends Thread
   {
       public void run()
      {
           System.out.println(this.getName());
      }
       public static void main(String[] args)
       {
           System.out.println(Thread.currentThread().getName());
           Thread1 thread1 = new Thread1();
           Thread1 thread2 = new Thread1 ();
           thread1.start();
           thread2.start();
      }
  }

The above code establishes two threads: thread1 and thread2. Lines 005 to 008 in the above code are the run methods of the thread1 class. When the start method is called on lines 014 and 015, the run method is automatically called. In line 007, this.getname () prints out the name of the current Thread. Since the Thread name was not specified when the Thread was created, the output Thread name is the default value of the system, in the form of thread-n. The thread name of the main thread is printed on line 011.

      The result of the above code is as follows:


main
Thread-0
Thread-1

As you can see from the output above, the main output on the first line is the name of the main thread. The following thread-1 and thread-2 are outputs of thread1 and thread2, respectively.

      Note: any Java program must have a main thread. In general, the name of the main thread is main. Only in the program to establish another thread, can be considered a true multithreaded program. That is, multithreaded programs must have more than one thread.

The Thread class has an overloaded constructor that sets the Thread name. In addition to using the constructor to set the Thread name when the Thread is created, you can modify the Thread name using the setName method of the Thread class. To set the Thread name through the constructor of the Thread class, you must use the public Thread (String name) constructor of the Thread class in the subclass of the Thread, so you must also add a constructor for passing in the Thread name in the subclass of the Thread. The following code gives an example of setting a thread name:


package mythread;

  public class Thread2 extends Thread
  {
      private String who;

      public void run()
      {
          System.out.println(who + ":" + this.getName());
      }
      public Thread2(String who)
      {
          super();
          this.who = who;
      }
      public Thread2(String who, String name)
      {
          super(name);
          this.who = who;
      }
      public static void main(String[] args)
      {
          Thread2 thread1 = new Thread2 ("thread1", "MyThread1");
          Thread2 thread2 = new Thread2 ("thread2");
          Thread2 thread3 = new Thread2 ("thread3");
          thread2.setName("MyThread2");
          thread1.start();
          thread2.start();
          thread3.start();
      }

There are two constructors in the class:

      Line 011: public sample2_2 (String who)

      This constructor has a parameter: who. this parameter identifies the currently established thread. The default constructor for Thread, public Thread (), is still called in this constructor.

      Line 016: public sample2_2 (String who, String name)

      The who in this constructor has the same meaning as the who in the first constructor, and the name parameter is the name of the thread. In this constructor, the public Thread (String name) constructor of the Thread class is called, which is super (name) on line 018.

      Three threads are established in the main method: thread1, thread2, and thread3. Thread1 sets the thread name by constructor, thread2 modifies the thread name by setName, and thread3 does not set the thread name.

      The operation results are as follows:


thread1:MyThread1
thread2:MyThread2
thread3:Thread-2

As you can see from the above output, the thread names of thread1 and thread2 have been changed, while the thread names of thread3 are still the default values: Thread-2. Thread3's Thread name is not thread-1, but thread-2, because thread-1 is already occupied when thread2 is created in line 024, so set thread3 to thread-2 when thread3 is created in line 025. Then change thread2's Thread name to MyThread2 on line 026.

      Note: before and after the start method can use the call elegantly-named setName setting thread, but the call after the start method using elegantly-named setName modified thread, produces uncertainty, that is to say, may be in the run method after the execution will perform elegantly-named setName. If want to use threads in the run method, can appear although calls the elegantly-named setName method, but the phenomenon of unmodified thread name.

      The start method of the Thread class cannot be called multiple times, for example, the thread1.start () method cannot be called twice. Otherwise it will throw a IllegalThreadStateException anomalies.


Related articles: