Java线程实现方式

最近开始复习线程知识,总结一下线程创建方式。

1、 创建线程的方法 之一 【继承Thread】

  • 1、创建Thread的子类
  • 2、重写该类的run方法
  • 3、调用该类的start();
/**
 * 创建线程的方法 之一 继承Thread
 * 
 * @see 1、创建Thread的子类
 * @see 2、重写该类的run方法
 * @see 3、调用该类的start();
 * 
 * @description:
 * @author: ArvinWoo
 * @date: 2019年7月17日上午10:46:02
 */
public class ThreadMethod extends Thread {

	@Override
	public void run() {
		for (long i = 0; i < 10; i++) {
			System.out.println(getName() + "____" + i);
		}
	}

	static class Test {
		public static void main(String[] args) {
			for (int i = 0; i < 50; i++) {
				System.out.println(Thread.currentThread().getName() + "____" + i);
				if (i == 20) {
					try {
						ThreadMethod t1 = new ThreadMethod();
						ThreadMethod t2 = new ThreadMethod();
						t1.start();
						t1.join();
						t2.start();
						t2.join();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}
	}
}

2、创建线程的方法 之二 【实现Runable接口】

  • 1、定义Runable接口的实现类
  • 2、重写该接口的run方法
  • 3、调用该类的start();

/**
 * 创建线程的方法 之一 实现Runable接口
 * 
 * @see 1、定义Runable接口的实现类
 * @see 2、重写该接口的run方法
 * @see 3、调用该类的start();
 * 
 * @description:
 * @author: ArvinWoo
 * @date: 2019年7月17日上午11:05:04
 */
public class RunableMethrod implements Runnable {

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName() + "____" + i);
		}
	}

	static class Test {

		public static void main(String[] args) {
			for (int i = 0; i < 50; i++) {
				System.out.println(Thread.currentThread().getName() + "____" + i);
				if (i == 20) {
					try {
						RunableMethrod rMethrod = new RunableMethrod();
						Thread t1 = new Thread(rMethrod, "新线程1");
						Thread t2 = new Thread(rMethrod, "新线程2");
						t1.start();
						t1.join();
						t2.start();
						t2.join();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}

		}

	}

}

3、创建线程的方法 之三 【实现Callable接口】

  • 1、创建Callable实现类的实例,使用FutureTask类来包装Callable对象, 该FutureTask对象封装了该Callable对象的call()方法的返回值。(FutureTask是一个包装器,它通过接受Callable来创建,它同时实现了Future和Runnable接口。)
  • 2、使用FutureTask对象作为Thread对象的target创建并启动新线程。
  • 3、调用FutureTask对象的get()方法来获得子线程执行结束后的返回值
/**
 * 创建线程的方法 之一 实现Callable接口
 * 
 * @see 1、创建Callable实现类的实例,使用FutureTask类来包装Callable对象,
 *      该FutureTask对象封装了该Callable对象的call()方法的返回值。
 *      (FutureTask是一个包装器,它通过接受Callable来创建,它同时实现了Future和Runnable接口。)
 * @see 2、使用FutureTask对象作为Thread对象的target创建并启动新线程。
 * @see 3、调用FutureTask对象的get()方法来获得子线程执行结束后的返回值
 * @description:
 * @author: ArvinWoo
 * @date: 2019年7月17日上午11:23:05
 */
public class CallableMethod implements Callable<Integer> {

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

	static class Test {
		public static void main(String[] args) {
			CallableMethod cMethod = new CallableMethod();
			FutureTask<Integer> fTask = new FutureTask<>(cMethod);
			for (int i = 0; i < 50; i++) {
				System.out.println(Thread.currentThread().getName() + "_____" + i);
				if (i == 20) {
					try {
						Thread t1 = new Thread(fTask, "FutureTask1");
						t1.start();
						t1.join();
						System.out.println("FutureTask 返回值是:"+fTask.get());
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
					
				}

			}
		}
	}

}

看下Callable方式 源码:

  • Callable 只有一个 方法call();
    在这里插入图片描述
  • 再看下FutureTask类 实现了RunableFuture接口
    在这里插入图片描述
  • 而RunableFuture接口又继承了Runable 和 Future 接口。
    又是熟悉的 java.lang.Runnable
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值