栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

快速搞懂线程实现的三种方式

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

快速搞懂线程实现的三种方式

一、实现线程的三种方式:         方式一、继承Thread类,复写run()方法
class AThread extends Thread {
	@Override
	public void run() {
		System.out.println("AThread:" + Thread.currentThread().getName());
		super.run();
	}
}
        方式二、实现Runnable接口,复写run()方法
class BThread implements Runnable {
	@Override
	public void run() {
		System.out.println("BThread:" + Thread.currentThread().getName());

	}
}
        方式三、实现Callable接口,复写call()方法
class CThread implements Callable {
	@Override
	public Object call() throws Exception {
		System.out.println("CThread:" + Thread.currentThread().getName());
		return Thread.currentThread().getName();
	}
} 
二、线程执行 
package ThreadStudy;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

public class T1 {
	public static void main(String[] args) throws Exception {
		// 方式一实现
		AThread t1 = new AThread();
		t1.start();

		// 方式二实现(一)
		Thread t2 = new Thread(new BThread());
		t2.start();

		// 方式二实现(二):匿名内部类
		Thread t3 = new Thread(new Runnable() {

			@Override
			public void run() {
				System.out.println("main:" + Thread.currentThread().getName());

			}
		});
		t3.start();

		// 方式三实现(一):线程池中执行Callable线程获取返回值
		ExecutorService exec = Executors.newSingleThreadExecutor();
		CThread ct1 = new CThread();
		Future f1 = exec.submit(ct1);
		System.out.println("方式三实现(一):" + f1.get());
		
		// 方式三实现(二):FutureTask类中执行Callable线程获取返回值
		CThread ct2 = new CThread();
		FutureTask f2 = new FutureTask(ct2);
		exec.submit(f2);
		System.out.println("方式三实现(二):" + f2.get());
		exec.shutdown();
	}
}

//方式一、继承Thread类,复写run()方法
class AThread extends Thread {
	@Override
	public void run() {
		System.out.println("AThread:" + Thread.currentThread().getName());
		super.run();
	}
}

//方式二、实现Runnable接口,复写run()方法
class BThread implements Runnable {

	@Override
	public void run() {
		System.out.println("BThread:" + Thread.currentThread().getName());

	}

}

//方式三、实现Callable接口,复写call()方法
class CThread implements Callable {

	@Override
	public Object call() throws Exception {
		System.out.println("CThread:" + Thread.currentThread().getName());
		return Thread.currentThread().getName();
	}

} 
三、Runnable执行成功后指定返回值 
可以指定任意类型的返回结果 Future submit(Runnable task, T result);

        代码实现

package ThreadStudy;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestTmp {
	public static void main(String[] args) throws Exception {

		ExecutorService exec = Executors.newSingleThreadExecutor();
		Future future = exec.submit(new Runnable() {

			@Override
			public void run() {
				System.out.println("Runnable开始执行");
			}
		}, "success");
		System.out.println("获取Runnable执行之后的返回结果:" + future.get());

	}
}
四、Callable与Runnable区别:
RunnableCallable
1、复写方法不同run()call()
2、是否带返回值
3、执行方式不同

既能在线程池中执行

又能在Thread类中执行

只能放在线程池中执行


 

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号