Creating a Java concurrent programming (a) thread

Creating a Java concurrent programming (a) thread

There are three ways to create a ready-made three each have their own advantages, each have their own characteristics. Flexibility in the project can be selected according to their needs.

Implementation of a

Implementation is achieved using a Runable interface to execute multiple threads, but there is a downside is run method of execution is no return value, nor can throw an exception.

public class ThreadOne implements Runnable {

    private int a = 10;

    @Override
    public void run() {
        System.out.println("线程" + Thread.currentThread().getName() + a);
    }
}

class Test {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(new ThreadOne());
        thread.start();
    }
}
复制代码

Thread class is created, and the object that implements the interface Runable passed to the Thread. Thread the start method call to execute threads.

Implement Method Two

Method Two Thread class inheritance to achieve employed because java is a single inheritance language, this class inherits the Thread class, so you can not inherit other classes. In addition, this method also can not get the return value of run method and the run method can not throw an exception.

public class ThreadTwo extends Thread {

    private int a = 10;

    @Override
    public void run() {
        System.out.println("线程" + Thread.currentThread().getName() + a);
    }
}

class TestTwo {
    public static void main(String[] args) throws Exception {
        ThreadTwo threadTwo = new ThreadTwo();
        threadTwo.start();
    }
}
复制代码

Implementation three

Method three to make up for a moment to realize the first two methods of disadvantage that thread can call to obtain the return value, call method can throw an exception, but the code is more complicated than the first two, by implementing Callable interface and then implement Callable incoming class interface configuration parameters through to FutureTask in, and then passed to the Future Thread by structural parameters, in fact FutureTask play is a transit role.

public class ThreadThree implements Callable {

    private int a = 10;

    @Override
    public Object call() throws Exception {
        synchronized (this){
            return ++a;
        }
    }
}

class TestThree {
    public static void main(String[] args) throws Exception{
        ThreadThree threadThree = new ThreadThree();

        for (int i = 0; i < 7; i++) {
            FutureTask<ThreadThree> threadThreeFutureTask = new FutureTask<ThreadThree>(threadThree);
            Thread thread = new Thread(threadThreeFutureTask);

            thread.start();
            System.out.println("线程返回值为:" + threadThreeFutureTask.get());

        }

        System.out.println("线程执行完毕");

    }
}
复制代码

to sum up

  1. Implementation and realization of a second approach is relatively simple compared with third implementation, but the drawback is unable to run method is no return value, run method can not throw an exception, but the third implementation can obtain call method returns a value acquisition, but also You may throw an exception.
  2. Implementation of a third implementation of the method is used in implementation of the interface, but is used in two ways to achieve the Thread class inheritance, which would be bound single inheritance java

Guess you like

Origin juejin.im/post/5e678d40f265da576d60b70d