Three methods of creating multithreading in Java

catalogue

1, Inherit the Thread class and override the run method

1. Prepare an ordinary student class

2. The student class inherits the Thread interface and overrides the run method

3. Generate two student threads in the Main method and start

4. Output results

2, Implement Runnable interface

1. Prepare an ordinary student class

2. Implement the Runnable interface and rewrite the run method

3. In the Main method, pass in the student class object as the constructor parameter of the Thread object and start it

4. Output results

3, Implement Callable interface

1. Prepare an ordinary student class

2. Implement the Callable interface and rewrite the call method

3. Create FutureTask and pass in the student class object implementing Callable as the parameter

4. Output results

1, Inherit the Thread class and override the run method

Example:

1. Prepare an ordinary student class

The student attributes include the student's name and the number of times the student jumps.

Methods

Construction method: initialize student name.

Jump behavior method: when the method is executed once, the number of times students jump is increased by 1, and the sentence is output.

public class StudentThread {
    private String name;
    private int jump;

    public StudentThread(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("My name is:" + this.name +",I jumped:" + ++jump + "lower");
    }
}

2. The student class inherits the Thread interface and overrides the run method

In the run method, we let the jump method execute 20 times, which also means that it will execute 20 times after the thread is started.

public class StudentThread extends Thread {
    private String name;
    private int jump;

    public StudentThread(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("My name is:" + this.name +",I jumped:" + ++jump + "lower");
    }

    @Override
    public void run() {
        for (int i = 0;i<20;i++)
        {
            this.Jump();
        }

    }
}

3. Generate two student threads in the Main method and start

public class Main {
    public static void main(String[] args) {
        StudentThread sh1 = new StudentThread("Lengshao");
        StudentThread sh2 = new StudentThread("KOMATSU");
        sh1.start();
        sh2.start();
    }
}

4. Output results

The alternate output of "Komatsu" and "lengshao" proves that the creation is successful

 

 

2, Implement Runnable interface

Example:

1. Prepare an ordinary student class

The student attributes include the student's name and the number of times the student jumps.

Methods

Construction method: initialize student name.

Jump behavior method: when the method is executed once, the number of times students jump is increased by 1, and the sentence is output.

public class StudentRunnable{
    private String name;
    private int jump;

    public StudentRunnable(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("My name is:" + this.name +",I jumped:" + ++jump + "lower");
    }
}

2. Implement the Runnable interface and rewrite the run method

Similar to overriding the run method of Thread

public class StudentRunnable implements Runnable {
    private String name;
    private int jump;

    public StudentRunnable(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("My name is:" + this.name +",I jumped:" + ++jump + "lower");
    }

    @Override
    public void run() {
        for (int i = 0;i<20;i++)
        {
            this.Jump();
        }

    }
}

3. In the Main method, pass in the student class object as the constructor parameter of the Thread object and start it

public class Main {
    public static void main(String[] args) {
        Thread thread1 = new Thread(new StudentRunnable("Lengshao"));
        Thread thread2 = new Thread(new StudentRunnable("KOMATSU"));
        thread1.start();
        thread2.start();
    }
}

4. Output results

The alternate output of "Komatsu" and "lengshao" proves that the creation is successful

 

3, Implement Callable interface

Example:

1. Prepare an ordinary student class

The student attributes include the student's name and the number of times the student jumps.

Methods

Construction method: initialize student name.

Jump behavior method: when the method is executed once, the number of times students jump is increased by 1, and the sentence is output.

public class StudentCallable{
    private String name;
    private int jump;

    public StudentCallable(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("My name is:" + this.name +",I jumped:" + ++jump + "lower");
    }
}

2. Implement the Callable interface and rewrite the call method

1. The return value of callable < T > t is the same as that of call method

2. Callable < T > t is the return type of get method of callable implementation class FutureTask

public class StudentCallable implements Callable<String> {
    private String name;
    private int jump;

    public StudentCallable(String name)
    {
        this.name = name;
    }

    private void Jump()
    {
        System.out.println("My name is:" + this.name +",I jumped:" + ++jump + "lower");
    }

    @Override
    public String call() {
        for (int i = 0;i<20;i++)
        {
            this.Jump();
        }
        return this.name + "It's over!";

    }
}

3. Create FutureTask and pass in the student class object implementing Callable as the parameter

Futuretask < T > t is the same as the call method return type of the object class that implements Callable

public class Main {
    public static void main(String[] args) {
        FutureTask<String> ft1 = new FutureTask<>(new StudentCallable("Lengshao"));
        FutureTask<String> ft2 = new FutureTask<>(new StudentCallable("KOMATSU"));
        Thread thread1 = new Thread(ft1);
        Thread thread2 = new Thread(ft2);
        thread1.start();
        thread2.start();
        try {
            String ft1_return = ft1.get();
            String ft2_return = ft2.get();
            System.out.println(ft1_return);
            System.out.println(ft2_return);
        }
        catch (InterruptedException | ExecutionException e)
        {
            e.printStackTrace();
        }

    }
}

4. Output results

The alternate output of "Komatsu" and "lengshao" proves that the creation is successful

Finally, the return value at the end of the thread is output

 

 

Above: it's a personal study note. If there is any error, you are welcome to point it out

Keywords: Java Multithreading intellij-idea

Added by five on Tue, 04 Jan 2022 06:34:39 +0200