Thread: Each task is called a thread. The thread cannot exist independently, it must be part of the process.
Single thread: Common Java applications are single threaded. For example, when running a helloworld program, the jvm process will be started, and then the main method will be run to generate a thread. The main method is also called the main thread.
Multithreading: A program that runs more than one thread at the same time is called a multithreaded program. Multithreading can meet the purpose of programmers to write efficient programs to achieve the goal of making full use of the CPU.
Single-threaded code example:
public class SingleThread {public static void main(String[] args){Thread thread = Thread.currentThread(); //Get the currently running thread object thread.setName("Single thread"); //Rename the thread.out.println(thread.getName()+"Running"); for(int i=0;i<10;i++){System.out.println("Thread is sleeping: "+i);try {thread.sleep(1000); //Thread is sleeping, delayed by one second} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("Thread error");}}}}}Multithreaded code example:
Note: There are two ways to implement multithreading, one is to inherit the Thread class, and the other is to implement the Runnable interface.
Inheriting Thread class to implement multi-threading
public class TestThread {public static void main(String[] args){ Thread t1 = new ExtendThread("t1",1000); //Create a thread using the up-turn object, and construct the thread name and thread sleep time Thread t2 = new ExtendThread("t2",2000); Thread t3 = new ExtendThread("t3",3000); t1.start(); //Start the thread and call the run method t2.start(); t3.start();}}class ExtendThread extends Thread{ //Inherit Thread's class String name;int time;public ExtendThread(String name, int time) { //Construct thread name and sleep time this.name=name; this.time=time;}public void run(){ //Rewrite the run method of the Thread class try{sleep(time); //All threads add sleep}catch(InterruptedExceptione){e.printStackTrace();System.out.println("Thread interrupt exception");}System.out.println("The name is: "+name+", thread sleeps: "+time+"msec"); }}Multithreading to implement Runnable interface
public class RunnableThread {public static void main(String[] args){Runnable r1=new ImplRunnable("r1",1000); //Runnable interface must rely on the Thread class to create threads Thread t1=new Thread(r1); //Runnable cannot call the start() method, because it is not a thread, so you need to use the Thread class to add threads Runnable r2=new ImplRunnable("r2",2000);Thread t2=new Thread(r2);Runnable r3=new ImplRunnable("r3",3000);Thread t3=new Thread(r3);t1.start(); //Start the thread and call the run method t2.start();t3.start();}}class ImplRunnable implements Runnable{ //Inheriting the Runnable interface String name;int time;public ImplRunnable(String name, int time) { //Construct the thread name and sleep time this.name = name;this.time = time;}@Overridepublic void run() { //Implement the run method try{Thread.sleep(time); //All threads add sleep}catch(InterruptedException e){e.printStackTrace();System.out.println("Thread interrupt exception");}System.out.println("name is: "+name+", thread sleeps: "+time+"ms");}}Note: The Thread class is actually a class that implements the Runnable interface.
Advantages of implementing Runnable interfaces over inheriting Thread classes