The Java multi-threaded (1)

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_36209121/article/details/77054885

Foreword

Java methods to achieve multi-threaded general, there are three, in accordance with normal use, it can be divided into two categories, one is no return value, and the other is the return value can be obtained, and today in this summary.

First, the need to obtain a return value

. A Thread class inheritance:

Multi-threaded implementation steps

  • Allowed to inherit the Thread class;
  • Then the run method override therein;
  • An object that implements the class;
  • call the object's start method, start the thread;
  • This is a thread object represents a thread, if you want to start multiple threads, implement multiple objects

code show as below:

public class ThreadMain01 {

    public static void main(String[] args) {
        ThreadTest t1 = new ThreadTest("01");
        ThreadTest t2 = new ThreadTest("02");
        t1.start();
        t2.start();
    }
}

class ThreadTest extends Thread {
    ThreadTest() {}
    ThreadTest(String name) {
        setName(name);
    }

    public void run() {
        System.out.println(getName()+" -- is running");
    }
}

. B implement Runnable:

In fact, to see the source code can be known, the Thread class implements Runnable interface that overrides the run method which is recommended to use this method to reduce coupling of the code.
Implementation steps:

  • Implement Runnable, rewriting run method;
  • Create an instance of an object;
  • Create Thread object instance, and the Runnable subclass object as a parameter passed to the object instance of Thread;
  • Thread object calls the start method to start the thread,

code show as below

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadMain02 {

    public static void main(String[] args) {
        ThreadRunnable01 tr = new ThreadRunnable01();

        new Thread(tr).start();
        new Thread(tr).start();
    }
}


class ThreadRunnable01 implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" -- is running");
    }
}

The above is a step to achieve the realization of multi-threaded through the Thread class, in normal use, we usually use the thread pool to open multiple threads, the following uses Executor framework;

  • Implement Runnable, rewriting run method;
  • Create a thread pool, access to ExecutorService objects;
  • Execute execute methods, or submit method (Runnable sub-class object as a parameter), Each call is equivalent to calling a new thread to perform the task;

code show as below


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadMain02 {

    public static void main(String[] args) {
        ThreadRunnable01 tr = new ThreadRunnable01();

        new Thread(tr).start();
        new Thread(tr).start();
        ExecutorService exe = Executors.newFixedThreadPool(5);
        for(int i=0; i<5; i++) {
            //exe.submit(tr);
            exe.execute(tr);
        }
    }
}


class ThreadRunnable01 implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+" -- is running");
    }
}

Guess you like

Origin blog.csdn.net/qq_36209121/article/details/77054885