多线程的三种实现
2016-05-26 17:22
295 查看
1.继承Thread类,重写run方法
2.实现Runnable接口
3.实现Callable接口
public class MyThread extends Thread{ //继承Thread会产生单继承局限,使用Runnable比较好 private String name; MyThread(String name){ this.name = name; } @Override public void run() { for(int i=0;i<10;i++) { System.out.println(this.name+" "+i); } } }
public static void main(String[] args) { MyThread t1 = new MyThread("thread1"); MyThread t2 = new MyThread("thread2"); MyThread t3 = new MyThread("thread3"); t1.start(); /* t1.start(); if (threadStatus != 0)//如果已經启动了,再次启动会抛这个异常 throw new IllegalThreadStateException(); */ t2.start(); t3.start(); }
2.实现Runnable接口
public class MyThread implements Runnable{ private String name; MyThread(String name){ this.name = name; } @Override public void run() { for(int i=0;i<10;i++) { System.out.println(this.name+" "+i); } } }
public static void main(String[] args) { MyThread t1 = new MyThread("thread"); //MyThread t2 = new MyThread("thread2"); //MyThread t3 = new MyThread("thread3"); Thread thread1 = new Thread(t1); Thread thread2 = new Thread(t1); Thread thread3 = new Thread(t1); thread1.start(); thread2.start(); thread3.start(); // Runnable r = ()->{ // for(int i=0;i<10;i++) { // System.out.println(i); // }}; // Thread t1 = new Thread(r); // t1.setName("thread1"); // Thread t2 = new Thread(r); // t2.setName("thread1"); // Thread t3 = new Thread(r); // t3.setName("thread1"); // t1.start(); // t2.start(); // t3.start(); }
3.实现Callable接口
public class MyCallable implements Callable{ //Callable接口比Runnable多了个返回值 //模拟卖票 int ticket = 10; @Override public String call() throws Exception { while(ticket > 0) { System.out.println("ticket has: "+ticket--); } return "ticket had sell done"; } }
public static void main(String[] args) throws InterruptedException, ExecutionException { Callable<String> cal = new MyCallable(); FutureTask<String> task = new FutureTask<String>(cal); Thread thread1 = new Thread(task); Thread thread2 = new Thread(task); thread1.start(); thread2.start(); System.out.println(task.get());//取得返回值 //以下是Lambda表达式实现 /* Callable<String> cal2 =()->{ for(int i=0;i<10;i++) { System.out.println(i); } return "success"; }; FutureTask<String> task2 = new FutureTask<String>(cal2); Thread thread11 = new Thread(task2); Thread thread22 = new Thread(task2); thread11.start(); thread22.start(); System.out.println(task2.get());//取得返回值 */ }
Callable接口的源码
@FunctionalInterface public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; }
总结:继承Thread会产生单继承局限,使用Runnable比较好,Callable接口比runnable接口多了个返回值 但最终都是通过Thread类的start启动线程
相关文章推荐
- 【C语言】03-第一个C程序代码分析
- 两台机器做互相信任 scp不使用密码
- 免费馅饼
- Android API DEMO:简单手绘游戏
- 四、MyBatis主配置文件
- WineBottler:让苹果Mac OS照样用IE浏览器
- RA, 寄存器分配
- Mybaits之逆向工程
- debug.keystore文件不存在解决办法
- JS学习的博客收藏
- java 接口练习题5
- Aquarium Cycling
- 《转载》什么变量在堆内存里存放,什么变量在栈内存里存放
- web工程下的有些路径的jsp可以直接在http url中输入进行显示,有些却不能显示??The requested resource is not available.
- iOS之UIWebView图文混排技术
- 热爱编程~~~
- Deep Learning Notes(2):overfit原因与办法
- nfs 安装教程 centos6
- 微信开发模板
- Windows下上传文件到Ubuntu的实现