Java multithreading--1, the creation of threads

        First of all, let's outline the principle of thread creation in Java. The creation of threads is created in jvm. Different jvms have different creation methods, (such as our most commonly used Oracle's hotspot jvm) and under jvm, it will also judge the current The system is what kind of system, so it is ultimately up to the system to create threads.

        There are several ways to create a thread in Java. This article mainly talks about the specific situation of starting a thread by calling the start method after the thread object is created. As for the several methods of creation, this article will not describe it.

        Let's first look at the source code of the constructor of the Thread class:

/**
 * Allocates a new {@code Thread} object. This constructor has the same
 * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
 * {@code (null, target, gname)}, where {@code gname} is a newly generated
 * name. Automatically generated names are of the form
 * {@code "Thread-"+}<i>n</i>, where <i>n</i> is an integer.
 *
 * @param  target
 *         the object whose {@code run} method is invoked when this thread
 *         is started. If {@code null}, this classes {@code run} method does
 *         nothing.
 */
public Thread(Runnable target) {
    init(null, target, "Thread-" + nextThreadNum(), 0);
}

        The constructor will directly call the private init method. There is such a sentence in the init method body: this.target = target; (target is the target object passed in the constructor), so the Runnable object passed by the constructor becomes the thread Object global variables.

private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc) {
    if (name == null) {
        throw new NullPointerException("name cannot be null");
    }

    this.name = name.toCharArray();
    // ... Some oth stmts...
    this.target = target;
    // ... Some oth stmts...
    /* Stash the specified stack size in case the VM cares */
    this.stackSize = stackSize;

    /* Set thread ID */
    tid = nextThreadID();
}

        When we call the start method of the thread, we will create a new thread through the system by calling the start method of the native, and then call the run method of the thread in this thread. Let's take a look at the source code of the run method:

@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

        Therefore, after the thread is created, it will judge whether the target object is empty, and if it is not empty, the run method of the Runnable object will be called. We can draw the following conclusions:

  1. The Runnable object is used as the parameter of the constructor, and when the thread executes, the run method of the object will be called.
  2. If the Thread class overwrites the run method and modifies the original run method, the run method of the Runnable object will not be called.

 

    Next: ThreadPoolExecutor details: https://my.oschina.net/u/3760321/blog/1615152

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325571153&siteId=291194637