thread线程创建的三种方式

方法1.创建类继承Thread类,调用.start()方法

public class NewThread1Test {

    private Log log = LogFactory.getLog(NewThread1Test.class);

    @Test
    public void test1(){
        new MyThread1().start();
        new MyThread2().start();
        new MyThread3().start();
        while (true){
        }
    }

    public class MyThread1 extends Thread{
        @Override
        public void run() {

            for (int i = 1; i < 101; i++) {
                log.info( Thread.currentThread().getName()+"//////////////////////////////////"+i);
            }
        }

    }

    public class MyThread2 extends Thread{

        @Override
        public void run() {

            for (int i = 1; i < 101; i++) {
                log.info( Thread.currentThread().getName()+"//////////////////////////////////"+i);
            }
        }

    }

    public class MyThread3 extends Thread{

        @Override
        public void run() {

            for (int i = 1; i < 101; i++) {
                log.info( Thread.currentThread().getName()+"//////////////////////////////////"+i);
            }
        }

    }

}

方法2.创建类实现Runnable接口,并将该类的对象作为参数创建Thread对象,调用.start()方法

public class NewThread2Test {

    private Log log = LogFactory.getLog(NewThread2Test.class);

    @Test
    public void test1(){
        new Thread(new MyRunnable1()).start();
        new Thread(new MyRunnable2()).start();
        new Thread(new MyRunnable3()).start();
        while (true){

        }
    }

    public class MyRunnable1 implements Runnable{

        @Override
        public void run() {

            for (int i = 100; i < 200; i++) {
                log.info( Thread.currentThread().getName()+"//////////////////////////////////"+i);
            }
        }

    }

    public class MyRunnable2 implements Runnable{

        @Override
        public void run() {

            for (int  i = 100; i < 200; i++) {
                log.info( Thread.currentThread().getName()+"//////////////////////////////////"+i);
            }
        }

    }

    public class MyRunnable3 implements Runnable{

        @Override
        public void run() {

            for (int  i = 100; i < 200; i++) {
                log.info( Thread.currentThread().getName()+"//////////////////////////////////"+i);
            }
        }

    }
}

方法3.创建类实现Callable接口,并以该类作为参数创建FutureTask<>对象,再将FutureTask<>对象作为参数创建Thread对象,调用.start()方法

注意:  1. Callable对象的call()方法有返回值,通过 FutureTask<>对象的get()方法接收;

2.下面的代码中主线程会一直执行"主线程继续执行==================================",会很快打印出来,但是在主线程中调用了ft.get(),后主线程会被子线程阻塞,直到子线程执行完,sum才会被打印.

 

 @Test
    public void test1() {

        FutureTask ft = new FutureTask<>(new MyCallable());
        new Thread(ft).start();
        System.out.println("主线程继续执行==================================");
        Integer sum = null;
        try {
            sum = ft.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println("sum="+sum);
    }

    public class MyCallable implements Callable{

        @Override
        public Integer call() throws Exception {
            int sum =0;

            for (int i = 0; i < 100; i++) {
                log.info(Thread.currentThread().getName()+"////////////////////////////"+i);
                sum += i;
            }

            return sum;
        }
    }
测试代码在GitHub
https://github.com/nokekang/springboot-scheduled.git项目下的test目录下

你可能感兴趣的:(开发)