1. Overview of construction methods

public Thread(a);
public Thread(Runnable target);
public Thread(String name);
public Thread(Runnable target, String name); / / the most commonly used
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);
Copy the code
  • Runnable target:RunnableThe implementation class or inheritsThreadThe subclass (ThreadAlso achievedRunnableInterface)
  • String name:Thread name
  • ThreadGroup group:Belongs to the currently created threadThread group
  • Long stackSize:The size of the thread stack is only explained roughly here, as followsJVMHave a relationship

2, the rounding

Note: All constructors call init at the bottom to initialize Thread. In other words, Thread has only one constructor. More on init later

public Thread(a) {
	init(null.null."Thread-" + nextThreadNum(), 0);
}
/* * The first parameter is ThreadGroup * the second parameter is Runnable * the third parameter is name * the fourth parameter is stackSize */
Copy the code

ThreadGroup: When this parameter isnullWill set this thread to the parent threadThreadGroupThe inside.

Thread parent = currentThread(); // Get the parent thread's ThreadGroup

// Set this thread to the parent thread's ThreadGroup
if (g == null) {
  g = parent.getThreadGroup();
}
Copy the code

Runnable: When this parameter isnullWhen this thread does nothing

@Override
public void run(a) {
   if(target ! =null) { target.run(); }}// To let the thread own the task and execute it, pass in the Runnable instance through the constructor, and override the run method
Copy the code

name: When this parameter isnull, will have a default name, named asThread- A number incrementing from 0

init(null.null."Thread-" + nextThreadNum(), 0);

/* For autonumbering anonymous threads. */
private static int threadInitNumber;
private static synchronized int nextThreadNum(a) {
  return threadInitNumber++;
}
Copy the code

stackSizeIf this parameter is null, the default value is 0, indicating that this parameter is ignored –> stackSize is highly platform dependent and can be ignored on some platforms