java创建线程的三种方式

23 篇文章 0 订阅
23 篇文章 1 订阅

java创建线程有三种方式:

1:实现Runnable接口(无返回值)
2:继承Thread类(无返回值)
3:使用ExecutorService、Callable、Future(有返回值)

实现Runnable接口

java 依靠接口来实现多态,所以在实现了Runnable接口的同时可以继承其他的类。Thread类的构造方法可以接受一个Runnable参数。

package tablejava;
class  SomeClass{
}
public class MyRunnable extends SomeClass implements Runnable{
    @Override
    public void run() {
        System.out.println("开始");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束");
    }
    public static void main(String[] args) {

         MyRunnable myRunnable=new MyRunnable();
         Thread thread=new Thread(myRunnable);
         thread.start();
         }
}

直接继承自Thread类

线程的启动必须依赖Thread类型,Thread类型本身也是实现了Runnable接口,但是由于java的单继承规则,它不能再继承其他的类。

package tablejava;

public class MyThread extends Thread{

    public void run() {
        System.out.println("开始");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束");
    }
    public static void main(String[] args) {
         MyThread myThread=new MyThread();
         myThread.start();
    }
}

使用Callable接口和FutureTask实现有返回值得线程

package tablejava;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;


public class MyCallable extends SomeClass implements Callable<Integer> {



    @Override
    public Integer call() throws Exception {
        System.out.println("开始");
        Thread.sleep(3000);
        System.out.println("结束");
        return 10;
    }
    public static void main(String[] args) {
       MyCallable myCallable=new MyCallable();
       FutureTask<Integer> Ft = new FutureTask<Integer>(myCallable);
       Thread mythread=new Thread(Ft);
       mythread.start();
       try {
        int a=Ft.get();
        System.out.println("the answer is "+a);
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }

    }
}

使用Callable接口和线程池实现带返回值得线程

package tablejava;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyCallable2 extends SomeClass implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("开始");
        Thread.sleep(3000);
        System.out.println("结束");
        return 10;
    }

    public static void main(String []args) {
        ExecutorService threadPool = Executors.newCachedThreadPool();
        CompletionService<Integer> cs = new ExecutorCompletionService<Integer>(threadPool);
        MyCallable2 mycallable2=new MyCallable2();
        cs.submit(mycallable2);
        int a;
        try {
            a = cs.take().get();
            System.out.println("the anwer is "+a);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
}

参考:
《java核心技术,卷I》
http://www.cnblogs.com/yezhenhan/archive/2012/01/09/2317636.html
http://www.cnblogs.com/baizhanshi/p/6425209.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值