The beginning of multithreading


Create thread

To understand multithreading, you must first know how to create a thread. There are three ways to create a thread: inherit the thread class, implement the runnable interface, and implement the callable interface.

//Create thread mode 1: inherit thread class, override run() method, call start to start thread
public class TestThread1 extends Thread {
    public void run() {
        //Run method thread body
        for (int i = 0; i < 20; i++) {
            System.out.println("I'm learning"+i);

    public static void main(String[] args) {
        //Main thread

        //Create a thread object
        TestThread1 testThread1 = new TestThread1();
        //Call the start() method to open the method

        for (int i = 0; i < 20; i++) {
            System.out.println("I'm learning multithreading"+i);
//Create thread mode 2: implement the runnable interface and rewrite the run method. The execution thread needs to drop the runnable interface implementation class and call the start method
public class TestThread3 implements Runnable{
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("I'm looking at the code");

    public static void main(String[] args) {

        //Creating implementation class object of runnable interface
        TestThread3 testThread3 = new TestThread3();
        //Create thread object, open our thread and proxy through thread object
        Thread thread = new Thread(testThread3);


        for (int i = 0; i < 20; i++) {
            System.out.println("I'm learning multithreading");

The third way is to use a downloaded jar package to demonstrate. Import the dependency first

//The third way of thread creation is to realize callable interface
//Import a dependency first
/ *
public class TestCallable implements Callable<Boolean> {

    private String url;
    private String name;

    public TestCallable(String url, String name) {
        this.url = url; = name;

    public Boolean call() throws Exception {
        webDownloader webDownloader = new webDownloader();
        System.out.println("Downloaded file for"+name);
        return true;

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable("Picture address", "1.jpg");
        TestCallable t2 = new TestCallable("Picture address", "2.jpg");
        TestCallable t3 = new TestCallable("Picture address", "3.jpg");

        //Create execution service:
        ExecutorService ser = Executors.newFixedThreadPool(3);
        //Submit execution service
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);

        //Get the results
        Boolean rs1 = r1.get();
        Boolean rs2 = r2.get();
        Boolean rs3 = r3.get();

        //Shut down the service

class webDownloader{

    public void downloader(String url,String name) {
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            System.out.println("I0 exception, there is a problem with the downloader method");


Thread unsafe

Deadlock condition

  • Mutex condition: a resource can only be used by one process at a time.
  • Request and hold condition: when a process is blocked by a request for resources, it holds the acquired resources.
  • The condition of not depriving: the resources obtained by the process can not be forcibly deprived before they are used up.
  • Cyclic waiting condition: a cyclic waiting resource relationship is formed between several processes.