preface

In JAVA, threads are represented by the Thread class, and all Thread objects must be instances of Thread or a subclass of Thread. Each thread’s job is to execute a sequence of code, and JAVA uses the thread body to hold this code. So, when we create a thread, we write the code that we put into the execution body of the thread based on the actual needs.

There are three creation methods

Create threads by inheriting the Thread class

Public class MyThread extends Thread {Override public void run() {super.run(); for (int i = 0; i < 10; I ++) {system.out.println (thread.currentThread ().getName() + "" + I); }}}Copy the code
Public static void main(String[] args) {MyThread MyThread = new MyThread(); // Give the thread a new name mythread.setName (" myThread "); // Start the thread mythread.start (); }Copy the code
MyThread 0 MyThread 1 MyThread 2 MyThread 3 MyThread 4 MyThread 5 MyThread 6 MyThread 7 MyThread 8 MyThread 9 MyThread 0 MyThread 1 MyThread 2 MyThread 3 MyThread 4 MyThread 5 MyThread 6 MyThread 7 MyThread 8 MyThread 9Copy the code

Create threads by implementing the Runable interface

Public class MyRunnable implements Runnable {run() @override public void run() {for  (int i = 0; i < 10; System.out.println(thread.currentThread ().getName() + "" + I); }}}Copy the code
Public static void main(String[] args) {MyRunnable MyRunnable = new MyRunnable(); public static void main(String[] args) {MyRunnable MyRunnable = new MyRunnable(); Thread = new Thread(myRunnable,"myRunnable"); // Start the child thread thread.start(); }Copy the code
// Result myRunnable 0 myRunnable 1 myRunnable 2 myRunnable 3 myRunnable 4 myRunnable 5 myRunnable 6 myRunnable 7 myRunnable 8 myRunnable 9Copy the code

Create an interface by implementing the Callable interface

Public class MyThread extends Thread {Override public void run() {super.run(); for (int i = 0; i < 10; I ++) {system.out.println (thread.currentThread ().getName() + "" + I); }}}Copy the code
Public static void main(String[] args) {// Create MyCallable MyCallable = new MyCallable(); // Use FutureTask to wrap myCallable objects, FutureTask<Integer> FutureTask = new; FutureTask = new FutureTask<Integer>(myCallable); Thread = new Thread(futureTask, "myCallable"); // Start the child thread thread.start(); Integer Integer = futureTask.get(); futureTask.get(); System.out.println(integer); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); }}Copy the code
// Result myCallable 0 myCallable 1 myCallable 2 myCallable 3 myCallable 4 myCallable 5 myCallable 6 myCallable 7 myCallable 8 myCallable 9 sum=45Copy the code

Of the three ways to create a Thread, the Runnable interface is the most common. Creating a Thread by inheriting the Thread class takes up inheritance bits. Because Java is single-inheritance, it is sometimes not convenient to create a Thread by implementing the Callable interface. And the operation step is more, also use less.

Six state

New State (new)

Thread Thread = new Thread();Copy the code

Runnable state

After startup, it is in a runnable state, executing in the Java Virtual machine // can be subdivided into: //1. Ready: waiting for the JVM to schedule after calling the start method //2 Running status: The JVM is scheduling this thread. thread.start();Copy the code

The state of being blocked

Blocked, unable to continue running, such as losing CPU execution permission

Waiting state

A thread that waits indefinitely for another thread to perform a particular operation cannot compete for CPU usage. It can only compete for CPU usage once another thread has performed a particular operation. For example, wait() requires notify() or notifyAll() to wake up join(), and must wait for other threads to finish.

Timed wait state (time_WAITING)

Thread.sleep(1000*1); Thread.wait(1000*1); // To wake up thread.join (1000*1), notify() or notifyAll(); ...........................Copy the code

Terminated state (terminated)

The execution is complete or terminated, and once it enters the terminated state, the thread dies and the thread life cycle ends.

The last

Thank you for reading here, after reading what do not understand, you can ask me in the comments section, if you think the article is helpful to you, remember to give me a thumbs up, every day we will share Java related technical articles or industry information, welcome to pay attention to and forward the article!