Java multithreaded programming USES the thread class to create threads

  • 2020-04-01 02:50:41
  • OfStack

There are two ways to create threads in Java: using the Thread class and using the Runnable interface. A Thread instance needs to be set up when using the Runnable interface. Therefore, whether a Thread is created through the Thread class or the Runnable interface, an instance of the Thread class or its subclasses must be created. The constructor of the Thread class has been overloaded eight times, 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 a class that implements the Runnable interface. Note that the Thread class also implements the Runnable interface, so an instance of a class inherited from the Thread class can also be passed into the constructor as a 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 the Thread's name is not set, the Thread USES the default Thread name: thread-n, where N is the order in which the Thread is created and is a positive integer that does not repeat.
ThreadGroup group
The group of threads to which the currently created thread belongs. If you do not specify a thread group, all threads are added to a default thread group. The details of thread groups are discussed in detail in a later section.
Long stackSize
The size of the thread stack, which is usually an integer multiple of the CPU page. For example, x86 has a page size of 4KB. On the x86 platform, the default thread stack size is 12KB.
A normal Java class can become a Thread class simply by inheriting from the Thread class. You can also execute the Thread code through the start method of the Thread class. Although subclasses of the Thread class can be instantiated directly, the Thread class's run method must be overridden in the subclass to actually run the Thread's code. The following code gives an example of creating a Thread using the Thread class:


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 lines 005 through above are the run method of the Thread1 class. When the start method is called on lines 014 and 015, the run method is automatically called. In line 007, this.getname () prints out the name of the current Thread. Since the Thread name was not specified when the Thread was created, the output Thread name is the default value of the system, in the form of thread-n. The thread name of the main thread is printed on line 011.
The result of the above code is as follows:
The main
Thread - 0
Thread 1
As you can see from the output above, the main output on the first line is the name of the main thread. The following thread-1 and thread-2 are outputs of thread1 and thread2, respectively.
Note: any Java program must have a main thread. The name of the main thread is usually main. Only in the program to establish another thread, can be considered a true multithreaded program. That is, multithreaded programs must have more than one thread.
The Thread class has an overloaded constructor that sets the Thread name. In addition to using the constructor to set the Thread name when the Thread is created, you can modify the Thread name using the setName method of the Thread class. 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 the Thread, so you must also add a constructor for passing in the Thread name in the subclass of the Thread. The following code gives an example of setting a thread name:


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 constructors in the class:
Line 011: public sample2_2(String who)
This constructor has one parameter: who. This parameter identifies the currently established thread. The default constructor for Thread, 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 created in the main method: thread1, thread2, and thread3. Thread1 USES the constructor to set the thread name, thread2 USES the setName method to modify the thread name, and thread3 does not set the thread name.
The operation results are as follows:


thread1:MyThread1
thread2:MyThread2
thread3:Thread-1

As you can see from the output above, the Thread names of thread1 and thread2 have changed, while the Thread name of thread3 remains the default: thread-1. The Thread Name of thread3 is not thread-2, but thread-1, because the Name of thread2 is already specified in line 026, so the Thread Name of thread3 is set to thread-1 when thread3 is started. So you get the output above.
Note: setName can be used to set the thread name before and after calling the start method, but using setName to change the thread name after calling the start method creates uncertainty, that is, setName may not be executed until the run method is finished. If the thread name is used in the run method, the thread name is not changed even though the setName method is called.
The start method of the Thread class cannot be called multiple times, for example, the thread1.start() method cannot be called twice. Otherwise it will throw a IllegalThreadStateException anomalies.


Related articles: