Java Thread: How to Create a Thread using the Thread class

Source: Internet
Author: User

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:

Copy codeThe Code 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:

Copy codeThe Code is 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 above Code creates two threads: thread1 and thread2. line 005 to line 008 in the above Code is the run method of thread1. 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:

Copy codeThe 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 this 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:

Copy codeThe Code is as follows: 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 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:

Copy codeThe Code is as follows: thread1: MyThread1
Thread2: MyThread2
Thread3: Thread-2

From the output, we can see that the Thread names of thread1 and thread2 have been modified, and the Thread name of thread3 is still the default value: the Thread name of the Thread-2.thread3 is NOT Thread-1, thread-2, because Thread-1 is occupied when thread2 is created in line 024. Therefore, when thread3 is created in line 025, the Thread name of thread3 is set to Thread-2. then, in row 026, the thread name of thread2 is changed to MyThread2. therefore, the above output result is displayed.

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.