There are two ways to create threads in Java: using the Thread class and using the Runnable interface. When using the Runnable interface, you need to create a Thread instance. Therefore, whether you create a thread through the Thread class or the Runnable interface, you must create an instance of the Thread class or its subclass. The constructor of the Thread class has been overloaded eight times. The constructor is as follows:
An instance of a class that implements the Runnable interface. It should be noted that the Thread class also implements the Runnable interface, therefore, instances of classes inherited from the Thread class can also be passed into this constructor as targets.
String name
The name of the thread. This name can be set through the setName method of the Thread class after creating a Thread instance. If the thread name is not set, the thread uses the default thread name: Thread-N. N is the order in which the thread is created and is a non-repeating positive integer.
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 thread groups will be discussed in detail in later chapters.
long stackSize
The size of the thread stack. This value is generally an integer multiple of the CPU page. For example, the page size of x86 is 4KB. Under the x86 platform, the default thread stack size is 12KB.
An ordinary Java class can become a thread class as long as it inherits from the Thread class. And the thread code can be executed through the start method of the Thread class. Although subclasses of the Thread class can be instantiated directly, the run method of the Thread class must be overridden in the subclass to actually run the thread's code. The following code gives an example of using the Thread class to create a thread:
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 results of running the above code are as follows:
Note: Any Java program must have a main thread. Generally, the name of this main thread is main. Only by creating other threads in the program can it be regarded as a true multi-threaded program. In other words, a multi-threaded program must have more than one thread.
The Thread class has an overloaded constructor that can set the thread name. In addition to using the constructor method to set the thread name when creating a thread, you can also use the setName method of the Thread class to modify the thread name. 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 Thread. Therefore, you must also add a thread for passing in the subclass of Thread. Name constructor. The following code gives an example of setting the thread name:
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();
}
Line 011: public sample2_2 (String who)
This constructor has one parameter: who. This parameter is used to identify the currently created thread. Thread's default constructor 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. Among them, thread1 sets the thread name through the construction method, thread2 modifies the thread name through the setName method, and thread3 does not set the thread name.
The running results are as follows:
Note: You can use setName to set the thread name before and after calling the start method. However, using setName to modify the thread name after calling the start method will cause uncertainty, which means that setName may not be executed until the run method is executed. If If you want to use the thread name in the run method, there will be a phenomenon that although the setName method is called, the thread name is not modified.
The start method of the Thread class cannot be called multiple times. For example, the thread1.start() method cannot be called twice. Otherwise an IllegalThreadStateException will be thrown.