Today’s sharing started, please give us more advice ~

Today I’m going to share with you a common approach to understanding threads, creating threads and threads.

Routine: to achieve slow output of characters

public class ThreadDemo1 { public static void main(String[] args) throws InterruptedException { String content = "People say you can't do it because they can't do it themselves. You have to protect your dreams as best you can. Those who laugh at you, they are bound to fail. They want to make you like them. If you got a dream, go get it, period." ; for (char item : content.toCharArray()) { System.out.print(item); Thread.sleep(200); }}}Copy the code

Advantage of multithreading – increased running speed

Take a look at the following multithreading routine to see how you can increase the speed of multithreading.

The code runs as follows:

Visible, multithreading advantage, can increase the running speed, shorten the running time.

How to create thread, touch thread (6 kinds of three types)

Class 1: Inherit Thread class to implement Thread creation (2 ways to create Thread)

Class 1: Inherits Thread class creation method 1

Public class ThreadDemo3 {static class MyThread extends Thread {@override public void run() {// The task performed by the Thread System.out.println(" thread.currentThread ().getName() "); }} public static void main(String[] args) {system.out.println (" thread.currentThread ().getName()); T1 = new MyThread(); // Run thread t1.start(); }}Copy the code

The code executes as follows

Class 1: Inherits Thread class creation method 2

Public class ThreadDemo4 {public static void main(String[] args) {system.out.println (" ThreadDemo4 "); " + Thread.currentThread().getName()); Thread Thread = new Thread() {@override public void run() {system.out.println (" Thread name:  " + Thread.currentThread().getName()); }}; thread.start(); }}Copy the code

The code executes as follows

Disadvantages of the first type of creation, which inherits the Thread class:

In the design of the Java language, only single inheritance can be implemented. If the class inherits from Thread, it cannot inherit from other classes.

Type 2: Implementation of the Runnable interface for thread creation (3 ways to create)

It addresses the weakness of the first class creation method, which is that Java cannot be multiinherited, but can implement multiple interfaces

Creation method ① :

The code executes as follows

Create method ② : use anonymous inner class to implement thread

Public class ThreadDemo6 {public static void main(String[] args) {system.out.println (" current thread name (thread name) : " + Thread.currentThread().getName()); Thread Thread = new Thread(new Runnable() {@override public void run() {system.out.println ();  " + Thread.currentThread().getName()); }}); thread.start(); }}Copy the code

The code executes as follows

Create method ③ : lambda + anonymous Runnable implementation

Public class ThreadDemo7 {public static void main(String[] args) {system.out.println (" current thread name (main thread name) : " + Thread.currentThread().getName()); Thread Thread = new Thread(() -> {system.out.println (" Thread name:  " + Thread.currentThread().getName()); }); thread.start(); }}Copy the code

The code executes as follows

Class 3: Implementing the Callable interface to implement thread creation (1 way to create)

The advantage is that you can get the result after the thread executes.

The code is executed as follows

Thread sleep (implemented in three ways)

Implementation method ① Advantage: high accuracy. Disadvantages: Code writing is complicated when the sleep time is too long.

public class ThreadDemo9 { public static void main(String[] args) throws InterruptedException { System.out.println(" start time: "+ new Date()); // Sleep for 1s thread.sleep (1000); System.out.println(" end time: "+ new Date()); }}Copy the code

The code execution results are as follows

When sleeping for a long time, implementation method ② and implementation method ③ can be used.

Implementation method ②

The code is executed as follows

Implementation method ③

public class ThreadDemo9 { public static void main(String[] args) throws InterruptedException { System.out.println(" start time: "+ new Date()); / / sleep 1 s Thread. Sleep (TimeUnit. SECONDS. ToMillis (1)); System.out.println(" end time: "+ new Date()); }Copy the code

The code is executed as follows

Typical example: Print “AABBCCDD” using two threads.

Use multithreading + thread sleep to complete the topic requirements

The code is executed as follows

summary

Process is the smallest unit of system resource allocation, thread is the smallest unit of system scheduling. Resources can be shared between threads within a process. Each process has at least one thread, the main thread.

Today’s share has ended, please forgive and give advice!