java多线程的3种方式

1.继承 Thread 类

通过在类上继承 Thread 类来实现多线程,Thread 类底层本质也是实现 Runnable 接口,它表示一个线程的实例,源代码如下所示:

public
class Thread implements Runnable {
    /* Make sure registerNatives is the first thing <clinit> does. */
    private static native void registerNatives();
    static {
        registerNatives();
    }

    private volatile String name;
    private int            priority;
    private Thread         threadQ;
    private long           eetop;

Thread 类内部有更丰富的方法,并且启动线程的唯一方法是通过Thread 类的 start() 方法。这种方式有一个缺点就是若当前类也需要继承其他父类则不能满足。因为一个类只能继承一个类,但可以实现多个接口。
代码示例:

public class ThreadClass extends Thread{

    @Override
    public void run() {
        try {
            Thread.sleep(100);
            System.out.println(Thread.currentThread().getId() + "继承了 Thread 类!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ThreadClass thread = new ThreadClass();
        ThreadClass thread1 = new ThreadClass();
        thread.start();
        thread1.start();
    }
}

2.实现 Runnable 接口方式实现多线程

若自己的类已经继承了其他类,就无法继承 Thread 类,此时就需要使用实现 Runnable 接口的方式实现多线程。
代码示例如下:

public class RunnableClass implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(100);
            System.out.println(Thread.currentThread().getId() + "继承了 Thread 类!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        RunnableClass thread = new RunnableClass();
        Thread thread1 = new Thread(thread);
        Thread thread2 = new Thread(thread);
        thread1.start();
        thread2.start();

    }
}

3.实现 Callable 接口

在jdk 1.5 以前实现线程只有上面两种方式,自从jdk 1.5 之后提供了Callable 和 Future ,通过他们可以在任务执行完毕后得到任务的执行结果。
代码示例如下:

public class CallableClass implements Callable {
    @Override
    public String call() throws Exception {
        try {
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "继承了 Thread 类!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "......";
    }

    public static void main(String[] args) throws Exception {
        CallableClass thread = new CallableClass();
        FutureTask<String> futureTask = new FutureTask<String>(thread);
  //      futureTask.run();
        String call = thread.call();
  //      String s = futureTask.get();
        System.out.println("结果:" + call);
    }
}

其中 FutureTask 实现了 Future 和 Runnable 接口,线程调用可使用 FutureTask.run() 方法执行。但是 Future 是具有阻塞性的,若call方法没有返回结果时,则当前线程一直被阻塞,会影响性能。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值