Java多线程

创建一个线程有三种方法:
1. 通过实现Runnable接口;
2. 通过继承Thread类本身;
3. 通过Callable和Future创建线程。

通过实现Runnable接口创建线程

我们先来通过实现Runnable接口来创建一个线程

package cn.xuzihui.util;

public class ThreadWithRunnableInterface {
    public static void main(String args[]) {
        RunnableDemo R1 = new RunnableDemo("Thread-1");
        R1.start();

        RunnableDemo R2 = new RunnableDemo("Thread-2");
        R2.start();
    }
}

/**
 *通过实现Runnable接口创建线程
 *创建一个线程,最简单的方法是创建一个实现Runnable接口类。为了实现Runnable,
 *一个类只需要执行一个方法调用run(),声明:public void run()
 *你可以重写该方法,重要的是理解的run()可以调用其他方法,使用其他类,并声明变量,就像主线程一样。
 *在创建一个实现Runnable()接口的类之后,你可以在类中实例化一个线程对象。
 *Thread定义了几个构造方法,下面的这个是我们经常使用的:
 *Thread(Runnable threadOb, String threadName);
 *这里,threadOb是一个实现Runnable接口的类的实例,并且threadName指定新线程的名字。
 *新线程创建之后,你调用它的start()方法它才会运行。
 *void start();
 */

class RunnableDemo implements Runnable{

    private Thread t;
    private String threadName;

    RunnableDemo(String name){
        threadName = name;
        System.out.println("Create " + threadName);
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println("Running " + threadName);
        try {
            for(int i = 4 ; i > 0 ; i--) {
                System.out.println("Thread:" + threadName + "," + i);
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            // TODO: handle exception
            System.out.println("Thread " + threadName + " interrupted.");
        }
        System.out.println("Thread " + threadName + " exiting.");
    }

    public void start() {
        System.out.println("Starting " + threadName);
        if(t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }

}

运行结果
这里写图片描述

通过继承Thread来创建线程
package cn.xuzihui.util;

public class ThreadWithExtendThread {
    public static void main(String args[]) {
        ThreadDemo T1 = new ThreadDemo("Thread-1");
        T1.start();

        ThreadDemo T2 = new ThreadDemo("Thread-2");
        T2.start();
    }
}

/**
 * 通过继承Thread来创建线程 
 * 创建一个线程的第二种方法是创建一个新的类,这个类继承Thread类,然后创建一个该类的实例。
 * 继承类必须重写run()方法,该方法是新线程的入口,它也必须调用start()方法才能执行。
 * 该方法尽管被列为一种多线程实现方式,但是本质上也是实现了Runnable接口的一个实例。
 */

class ThreadDemo extends Thread{

    private Thread t;
    private String threadName;

    ThreadDemo(String name){
        threadName = name;
        System.out.println("Createing " + threadName);
    }

    public void run() {
        System.out.println("Running " + threadName);
        try {
            for(int i = 4 ; i > 0 ; i--) {
                System.out.println("Thread:" + threadName + "," +i);
                //让线程睡眠一会
                Thread.sleep(50);
            }
        } catch (InterruptedException e) {
            // TODO: handle exception
            System.out.println("Thread " + threadName + " interrupted.");
        }
        System.out.println("Thread " + threadName + " exiting.");
    }

    public void start() {
        System.out.println("Starting " + threadName);
        if(t == null) {
            t = new Thread(this, threadName);
            t.start();
        }
    }
}

运行结果
这里写图片描述

通过Callable和Future创建线程
package cn.xuzihui.util;

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

/**
 * 1. 创建Callable接口的实现类,并实现call()方法,该call()方法将作为线程执行体,并且有返回值。
 * 2. 创建Callable实现类的实例,使用FutureTask类来包装Callable对象,该FutureTask对象
 *    封装了该Callable对象的call()方法的返回值。
 * 3. 使用FutureTask对象作为Thread对象的target创建并启动新线程。
 * 4. 调用FutureTask对象的get()方法来获得子线程执行结束后的返回值。
 */
public class ThreadWithCallableInterface implements Callable<Integer> {

    public static void main(String args[]) {
        ThreadWithCallableInterface tcf = new ThreadWithCallableInterface();
        FutureTask<Integer> ft = new FutureTask<>(tcf);
        for(int i = 0 ; i < 100 ; i++) {
            System.out.println(Thread.currentThread().getName()+" 的循环变量i的值 " + i);
            if(i == 20) {
                new Thread(ft, "有返回值的线程").start();
            }
        }
        try {
            System.out.println("子线程的返回值:" + ft.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Integer call() throws Exception {
        int i = 0;
        for( ; i < 100 ; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
        return i;
    }

}
创建线程的三种方式的对比
  1. 采用实现Runnable、Callable接口的方式创建多线程时,线程类知识实现了Runnable接口或者Callable接口,还可以继承其他类。
  2. 使用继承Thread类的方式创建多线程时,编写简单,但是如果需要访问当前线程,则无需使用Thread.currentThread()方法,直接使用this即可获取当前线程。

文章摘自菜鸟教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值