java creates basic multithreading

Posted by Tibster on Mon, 07 Oct 2019 12:11:30 +0200

There are basically two common ways to create multithreads in java

The first is a method implementation that inherits the Thread class.

The second is the implementation of Runnable interface.

Look at the code.

Let's look at the implementation of the first method of inheriting the Thread class

package com.example.demo.com.utils;
/**
*
* @ClassName:ThreadUtil.java
* @description:
* @author:
* @date: 
* @version: v1.0.0
*
**/
public class ThreadUtil extends Thread {
	
	
	@Override
	public void run() {
        /**
         * Write a program to run
         */
	while (true) {
            System.out.println(" What is currently running is"+Thread.currentThread().getName()+"thread"); // Print the name of the current thread
            try {
                Thread.sleep(1000); // Rest for 1 second.
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
    	ThreadUtil tu1 = new ThreadUtil(); //Create the first thread
        tu1.start(); // Startup thread
        ThreadUtil tu2 = new ThreadUtil(); //Create a second thread
        tu2.start(); // Startup thread
    }
}

Operation result

But the thread names are Thread - numbers, so by the way, you can change the name of the thread if you don't know the key content.

package com.example.demo.com.utils;
/**
*
* @ClassName:ThreadUtil2.java
* @description:
* @author: 
* @date: 
* @version: v1.0.0
*
**/
public class ThreadUtil2 extends Thread {
    
	
	@Override
	public void run() {
        /**
         * Write a program to run
         */
	while (true) {
            System.out.println(" What is currently running is"+Thread.currentThread().getName()+"thread"); // Print the name of the current thread
            try {
                Thread.sleep(1000); // Rest for 1 second.
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Create the name of the thread that makes up the rename
     * 
     * @param name
     */
    public ThreadUtil2(String name) {
        super(name);
    }

    public static void main(String[] args) {

    	ThreadUtil2 tu1 = new ThreadUtil2("tu1");//Create the first thread and rename it
        ThreadUtil2 tu2 = new ThreadUtil2("tu2");//Create a second thread and rename it
        tu1.start(); // Startup thread
        tu2.start(); // Startup thread
    }
	
	
}

Look at the second way to implement the Runnable interface.

package com.example.demo.com.utils;
/**
*
* @ClassName:ThreadUtil2.java
* @description:
* @author: 
* @date: 
* @version: v1.0.0
*
**/
public class ThreadUtil3 implements Runnable {

    @Override
    public void run() {
        while(true) {
        	System.out.println(" What is currently running is"+Thread.currentThread().getName()+"thread"); // Print the name of the current thread
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {

    	ThreadUtil3 tu = new ThreadUtil3(); // Instantiate Thread Task Class
        Thread t1 = new Thread(tu);// Create the first thread object and pass in the thread task class as a constructor parameter
        Thread t2 = new Thread(tu);// Create a second thread object and pass in the thread task class as a constructor parameter
        t1.start(); // Startup thread
        t2.start(); // Startup thread
    }

}

Operation result

Basic usage is basically like this.

Note: Besides the threads created, there are threads of main method running, so the number of threads is more than the number of threads created.

Topics: Java REST