Java multithreaded programming using the thread class to create threads _java

Source: Internet
Author: User
Tags thread class

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, such as 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 row in the above code is 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:
Main
Thread-0
Thread-1
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. Generally this main thread's name is main. Only if you build another thread in your program can you be 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 by 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-1

As you can see from the output above, the thread names for Thread1 and thread2 have been modified, and the THREAD3 thread name is still the default: Thread-1. The THREAD3 thread name is not Thread-2, but Thread-1 because the name of THREAD2 is already specified on line 026, so the THREAD3 thread name is set to Thread3 when the Thread-1 is started. So you get the output from the 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, which means that you may not perform the setname until the Run method has finished executing. If you want to use the thread name in the Run method, there is a phenomenon where 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.

Related Article

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.