thread related

  A thread refers to an execution flow in a process, and a process can run multiple threads. For example, the java.exe process can run many threads.

A thread always belongs to a process, a thread does not have its own virtual address space and shares all resources allocated to the process with other threads within the process.


Threads are still different from processes during execution. Each independent thread has an entry for program execution, a sequential execution sequence, and an exit for the program. However, threads cannot be executed independently, and must depend on the application program, and the application program provides multiple thread execution control.

A thread is an entity of a process and the basic unit of CPU scheduling and dispatch. It is a basic unit that is smaller than a process and can run independently. A thread basically does not own system resources by itself, but only has a few resources that are necessary in operation (such as a program counter, a set of registers and stacks), but it can share all the resources owned by the process with other threads belonging to the same process.

Threads have their own stacks and local variables. There is no separate address space between single threads. A thread contains the following contents:

an instruction pointer to the currently executed instruction;

a stack;

A collection of register values ​​that define a subset of values ​​that describe the processor state of the executing thread

A private data area.

  Java thread : create and start

First, define the thread

1. Extend the java.lang.Thread class

  There is a run() method in this class, you should pay attention to its usage: public void run()

        If the thread was constructed using a standalone Runnable run object, the run method of the Runnable object is called; otherwise, the method does nothing and returns.

        Subclasses of Thread should override this method.

2. Implement the java.lang.Runnable interface

  void run()

        When a thread is created with an object that implements the interface Runnable, starting the thread causes the object's run method to be called in a separate thread of execution.

        The general contract of the method run is that it may perform any desired action.

Second, instantiate the thread

   1. If it is a thread that extends the java.lang.Thread class, it can be directly new.

        2. If it is a class that implements the java.lang.Runnable interface, use the construction method of Thread:

Thread(Runnabletarget)
Thread(Runnabletarget, String name)
Thread(ThreadGroupgroup, Runnable target)
Thread(ThreadGroupgroup, Runnable target, String name)
Thread(ThreadGroupgroup, Runnable target, String name, long stackSize)

     in:

        Runnable target: An instance of a class that implements the Runnable interface. 

  1. The Thread class also implements the Runnable interface, so instances of classes inherited from the Thread class can also be passed into this constructor as targets.
  2. An instance of a class that directly implements the Runnable interface.
  3. The thread pool creates multiple threads.

        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 created. Default thread name: Thread-N, N is the order in which threads are created, and is a non-repeating positive integer.

        ThreadGroup group: The thread group to which the currently established thread belongs. If no thread group is specified, all threads are added to a default thread group.

        long stackSize: The size of the thread stack, this value is generally an integer multiple of the CPU page. For example, the page size of x86 is 4KB. Under the x86 platform, the default thread stack size is 12KB.

3. Start the thread

  Call the start() method on the thread's Thread object, not run() or other methods.

        Before calling the start() method: the thread is in a new state, which means that there is a Thread object, but there is no real thread.

        After calling the start() method: a series of complicated things happen -

        start a new thread of execution (with a new call stack);

        该线程从新状态转移到可运行状态;

        当该线程获得机会执行时,其目标run()方法将运行。

        注意:对Java来说,run()方法没有任何特别之处。像main()方法一样,它只是新线程知道调用的方法名称(和签名)。因此,在Runnable上或者Thread上调用run方法是合法的。但并不启动新的线程。


Guess you like

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