java创建线程的三种方式

一、实现Runnable接口

  1. 步骤:
    (1)创建自定义类并实现Runable接口
    (2)实例化自定义的类
    (3)将自定义的类的实例作为参数传给Thread类,创建thread实例
    (4)调用thread实例的start()方法,启动子进程
  2. 代码:
class ThreadDemo implements Runnable {  //实现Runnable接口

    //重写run方法
    @Override
    public void run() {
        //Thread.currentThread().getName()  获取当前运行线程的名称
        System.out.println(Thread.currentThread().getName()+"subThread starting ...");
        int i = 0;
        while (!Thread.currentThread().isInterrupted()){
            System.out.println(i++);
        }
        System.out.println(Thread.currentThread().getName()+"subthread end... ");
    }
}
public class Demo {
    public static void main(String[] args) {

        System.out.println(Thread.currentThread().getName() + "main Thread Starting");
        RunnableThread runnableThread = new RunnableThread();
        Thread thread = new Thread(runnableThread,"A");
        thread.start();

        System.out.println(Thread.currentThread().getName() + "main Thread end");
    }
}

二、继承Thread类

  1. 步骤:
    (1)创建自定义类并继承Thread类,并且重写run()方法
    (2)实例化自定义的类
    (3)通过实例化对象调用start方法来创建新线程
  2. 代码:
class ThreadDemo extends Thread {  //继承Thread类

    private Thread thread;

    public ThreadDemo(Thread thread) {
        this.thread = thread;
    }
    public  ThreadDemo(){
        this.thread = null;
    }

    //重写thread类的run方法
    @Override
    public void run() {
        if(thread != null) {
            try {
                this.thread.join(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"running");

    }
}
public class Demo {
    public static void main(String[] args) {

        System.out.println(Thread.currentThread().getName() + "main Thread Starting");
        ThreadDemo threadDemo = new ThreadDemo();
        threadDemo.start();
        System.out.println(Thread.currentThread().getName() + "main Thread end");
    }
}

三、实现Callable接口

  1. 步骤: (Callable接口的实现是线程池提供的一种创建线程的方式)
    (1)实现Callable接口,并且重写call方法,该方法为线程执行体
    (2)创建线程池(Executors工具类提供的方法创建线程池)
    (3)创建Callable接口实现类的实例
    (4)将实例对象通过线程池的submit方法提交给线程池从而创建新的线程
  2. 代码:
class CallableDemo implements Callable {

    @Override
    public Object call() throws Exception {
        System.out.println("Call() start...");
      return null;
    }
}
public class Demo {

    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName() + "main Thread Starting......");
        ExecutorService excutorService = Executors.newSingleThreadExecutor(); //创建线程池
        CallableDemo callableDemo =  new CallableDemo();  //创建实例
        Future<Integer> future =excutorService.submit(callableDemo); //通过线程池的submit方法创建新的线程
         try {
            Integer integer = future.get();
            future.cancel(true);
            System.out.println(integer);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"  main thread end...");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值