Blogjava
There are two ways to create a Thread in Java: using the Thread class and using the Runnable interface. You must create a Thread instance when using the Runnable interface. Therefore, both the Thread class and Runnable interface must be used to create an instance of the Thread class or its subclass. The Thread class constructor is overloaded eight times. The constructor is 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 );
Runnable target
An instance of the class that implements the Runnable interface. Note that the Thread class also implements the Runnable interface. Therefore, instances of classes inherited from the Thread class can also be used as the target to pass in this constructor.
String name
The name of the thread. This name can be set through the setName method of the Thread class after the Thread instance is created. If the Thread name is not set, the Thread uses the default Thread name: Thread-N, N is the sequence established by the Thread, and is a positive integer that is not repeated.
ThreadGroup group
The thread group to which the currently created thread belongs. If no thread group is specified, all threads are added to a default thread group. Details about the thread group will be discussed in later sections.
Long stackSize
The size of the thread stack, which is generally an integer multiple of the CPU page. For example, the page size of x86 is 4 kb. On the x86 platform, the default thread stack size is 12 kb.
A common Java class can be a Thread class as long as it inherits from the Thread class. The Thread code can be executed through the start method of the Thread class. Although the sub-classes of the Thread class can be directly instantiated, the sub-classes must overwrite the run method of the Thread class to truly run the Thread code. The following code provides an example of using the Thread class to create a Thread:
001 package mythread;
002
003 public class Thread1 extends Thread
004 {
005 public void run ()
006 {
007 System. out. println (this. getName ());
008}
009 public static void main (String [] args)
010 {
011 System. out. println (Thread. currentThread (). getName ());
012 Thread1 thread1 = new Thread1 ();
013 Thread1 thread2 = new Thread1 ();
014 thread1.start ();
015 thread2.start ();
016}
017}
The above Code creates two threads: thread1 and thread2. Line 005 to line 008 in the above Code is the run method of Thread1 class. When the start method is called in lines 014 and 015, the system automatically calls the run method. Use this. getName () outputs the name of the current Thread. Because no Thread name is specified during Thread creation, the output Thread name is the default value of the system, that is, the Thread-n format. Output the main thread name in the 011 line.
The running result of the above Code is as follows:
Main
Thread-0
Thread-1
The output result shows that the main output in the first line is the name of the main thread. Thread-1 and Thread-2 are output results of thread1 and thread2 respectively.
Note: any Java program must have a main thread. Generally, the name of the main thread is main. Only by establishing another thread in the program can it be regarded as a real multi-threaded program. That is to say, a multi-threaded program must have more than one thread.
The Thread class has an overload constructor that can set the Thread name. In addition to setting the Thread name when creating a Thread using the constructor, you can also use the setName method of the Thread class to modify the Thread name. To set the Thread name through the Thread class constructor, you must use the public Thread (String name) constructor of the Thread class in the Thread subclass. Therefore, you must add a constructor to the subclass of Thread to pass in the Thread name. The following code provides an example of setting the thread Name:
001 package mythread;
002
003 public class Thread2 extends Thread
004 {
005 private String who;
006
007 public void run ()
008 {
009 System. out. println (who + ":" + this. getName ());
010}
011 public Thread2 (String who)
012 {
013 super ();
014 this. who = who;
015}
016 public Thread2 (String who, String name)
017 {
018 super (name );
019 this. who = who;
020}
021 public static void main (String [] args)
022 {
023 Thread2 thread1 = new Thread2 ("thread1", "MyThread1 ");
024 Thread2 thread2 = new Thread2 ("thread2 ");
025 Thread2 thread3 = new Thread2 ("thread3 ");
026 thread2.setName ("MyThread2 ");
027 thread1.start ();
028 thread2.start ();
029 thread3.start ();
030}
031
There are two constructor methods in the class:
Row 3: public sample2_2 (String who)
This constructor has a parameter: who. This parameter is used to identify the currently created thread. In this constructor, the default constructor public Thread () of Thread is still called ().
Row 3: 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, that is, the super (name) of row 018th ).
Three threads are created in the main method: thread1, thread2, and thread3. Thread1 sets the thread name through the constructor, thread2 modifies the thread name through the setName method, and thread3 does not set the thread name.
The running result is as follows:
Thread1: MyThread1
Thread2: MyThread2
Thread3: Thread-1
From the output, we can see that the Thread names of thread1 and thread2 have been modified, and the Thread names of thread3 are still the default value: Thread-1. The Thread Name of thread3 is NOT Thread-2, but Thread-1. This is because the Name of thread2 has been specified in line 026. Therefore, when thread3 is started, the Thread name of thread3 is set to Thread-1. Therefore, the above output result is obtained.
Note: You can use setName to set the thread name before and after the start method is called, but modifying the thread name using setName after the start method is called will produce uncertainty, that is to say, setName may be executed only after the run method is executed. If the thread name is used in the run method, the thread name is not modified even though the setName method is called.
The start method of the Thread class cannot be called multiple times. For example, you cannot call the thread1.start () method twice. Otherwise, an IllegalThreadStateException is thrown.