There are two ways to create threads in Java: Using the thread class and using the Runnable interface. You need to create a thread instance when using the Runnable interface. Therefore, whether you create a thread through the thread class or the Runnable interface, you must establish an instance of the thread class or its subclass. The thread class construction method is overloaded eight times and is constructed as follows:
Copy Code code 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, so instances of classes that inherit from the thread class can also be passed into this constructor as 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 you do not set the name of a thread, the thread uses the default thread name: Thread-n,n is the order in which the thread is established, and is a positive integer that is not duplicated.
Threadgroup Group
The thread group that the currently established thread belongs to. If you do not specify a thread group, all threads are added to a default thread group. Details about the thread group are discussed in detail in later chapters.
Long stacksize
The size of the line stacks, which is typically the integer multiple of the CPU page. The page size of the x86 is 4KB. Under the x86 platform, the default thread stack size is 12KB.
A normal Java class can be a thread class as long as it inherits from the thread class. Thread code can also be executed through the start method of the thread class. Although subclasses of the thread class can be instantiated directly, it is necessary to overwrite the thread class's Run method in the subclass to actually run the threads ' code. The following code gives an example of using the thread class to create threads:
Copy Code code as follows:
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 code above establishes two threads: Thread1 and Thread2. The 005 to 008 lines in the preceding code are the Run method of the Thread1 class. When you call the Start method on lines 014 and 015, the Run method is automatically invoked by the system. The name of the current thread is exported using This.getname () on line 007, because the thread name is not specified when the thread is established, so the thread name that is exported is the system's default value, which is the form of the thread-n. The thread name for the main thread is exported on line 011.
The results of the above code run as follows:
Copy Code code as follows:
From the output above, it can be seen that the first line of output main is the name of the main thread. The following Thread-1 and Thread-2 are the outputs of thread1 and Thread2 respectively.
Note: Any Java program must have a main thread. Typically, the name of this main thread is main. Only the other thread in the program can be considered a real multithreaded program. In other words, multithreaded routines must have more than one thread.
The thread class has an overloaded constructor method that can set the name of the threads. In addition to using the constructor method to set the thread name when creating a thread, you can also modify the thread name by using the SetName method of the thread class. To set the thread name through the construction method of the thread class, you must use the thread class's public thread (String name) construction method in the child class of thread, so you must also add a construction method for the incoming thread name in the subclass of thread. The following code gives an example of setting a thread name:
Copy Code code as follows:
Package mythread;
public class Thread2 extends Thread
{
Private String who;
public void Run ()
{
System.out.println (who + ":" + this.getname ());
}
Public Thread2 (the 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 construction methods in the class:
Line No. 011: Public sample2_2 (String who)
This construction method has one parameter: WHO. This parameter is used to identify the currently established thread. The default construction method public thread () of thread is still called in this constructor.
Line No. 016: Public sample2_2 (String who, string name)
The WHO in this construction method has the same meaning as the WHO in the first construction method, and the name parameter is the name of the thread. The public thread (String name) construction method of the Thread class is called in this constructor, which is the super (name) of line No. 018.
Three threads were created in the Main method: Thread1, Thread2, and Thread3. Where Thread1 sets the thread name by constructing the method, Thread2 modifies the thread name through the SetName method, thread3 the thread name is not set.
The results of the operation are as follows:
Copy Code code as follows:
Thread1:mythread1
Thread2:mythread2
Thread3:thread-2
As you can see from the output above, the thread names for Thread1 and thread2 have been modified, The THREAD3 thread name is still the default: Thread-2.thread3 's thread name is not Thread-1, but Thread-2, because Thread2 is already occupied when the 024 line is established, so Thread-1 is created when 025 lines are established THREAD3 The EAD3 thread name is set to Thread-2. Then the thread2 's thread name is modified to MyThread2 in line 026. So you get the output above.
Note: You can use SetName to set the thread name before and after calling the Start method, but using SetName to modify the thread name after invoking the Start method can create uncertainty. That is, it is possible to execute the SetName after the Run method has been executed. If you want to use the thread name in the Run method, a phenomenon occurs that the SetName method is invoked, but the thread name is not modified.
The start method of the thread class cannot be invoked multiple times, such as the two Thread1.start () method cannot be invoked. Otherwise, a Illegalthreadstateexception exception is thrown.