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<Integer> 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<Integer>{

        @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目录下
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值