一 线程的创建

线程创建的四种方式

  1. 继承Thread类、
  2. 实现Runnable接口、
  3. 实现ThreadFactory接口、
  4. 使用ExecutorService、Callable、Future实现有返回结果的多线程

1. 继承Thread类

public class MyThread extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + i);
            System.out.println(this.getId());
        }
    }

    public static void main(String[] args) {
        System.out.println("begin main");
        MyThread myThread = new MyThread();
        myThread.start();
        System.out.println("end main");
    }
}

2. 实现Runnable接口

public class MyRunnable implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + ":" + i);
            System.out.println(Thread.currentThread().getId());
        }
    }

    public static void main(String[] args) {
        System.out.println("begin main");
        Thread thread = new Thread(new MyRunnable());
        thread.start();
        System.out.println("end main");
    }
}

3. 实现ThreadFactory接口

public class MyThreadFactory implements ThreadFactory {

    @Override
    public Thread newThread(Runnable r) {
        return new Thread(r);
    }

    public static void main(String[] args) {
        MyThreadFactory myFactory = new MyThreadFactory();
        System.out.println("begin main");
        Thread thread = myFactory.newThread(new MyThread());
        thread.start();
        System.out.println("end main");
    }
}

4.使用ExecutorService、Callable、Future实现有返回结果的多线程

public class MyCallable implements Callable<Object> {
	
	private String name;
	
	public MyCallable(String name) {
		this.name = name;
	}

	@Override
	public Object call() throws Exception {
		return name;
	}
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		int taskSize = 5;
		// 创建一个线程池
		ExecutorService pool = Executors.newFixedThreadPool(taskSize);
		// 创建多个有返回值的任务
		List<Future> list = new ArrayList<Future>();
		for (int i = 0; i < taskSize; i++) {
			Callable c = new MyCallable("线程" + i);
			// 执行任务并获取Future对象
			Future f = pool.submit(c);
			// System.out.println(">>>" + f.get().toString());
			list.add(f);
		}
		// 关闭线程池
		pool.shutdown();

		// 获取所有并发任务的运行结果
		for (Future f : list) {
			// 从Future对象上获取任务的返回值,并输出到控制台
			System.out.println(">>>>" + f.get().toString());
		}
	}
}

区别

使用第一种方式创建的线程,可以直接使用this调用线程的方法。(this->new MyThread())<创建Thread对象>

使用第二种方式创建的线程,不能直接使用this调用线程方法,MyRunnable只是重写了Runnable接口的run方法,而真正的线程是new Thread(new MyRunnable())时创建,MyRunnable作为Thread构造方法的参数。<用Thread构造方法创建对象>

使用第三种方法创建的线程,借助线程工厂创建,可批量生产一定规格的线程<用ThreadFactory的newThread方法创建对象>

使用第四种方式创建的线程,ExecutorService、Callable、Future这个对象实际上都是属于Executor框架中的功能类,返回结果的线程是在JDK1.5中引入的新特征,确实很实用,有了这种特征我就不需要再为了得到返回值而大费周折了,而且即便实现了也可能漏洞百出。


可返回值的任务必须实现Callable接口,类似的,无返回值的任务实现Runnable接口。执行Callable任务后,可以获取一个Future的对象,在该对象上调用get就可以获取到Callable任务返回的Object了,再结合线程池接口ExecutorService就可以实现传说中有返回结果的多线程了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sxjxrxm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值