创建线程由三种方式: (1)继承Thread类 (2)实现Runnable接口 (3)实现Callable接口 代码示例: 1、继承Thread类 1 package com.sxt.thread; 2 3 /** 4 * @ClassName StartThread 5 * 创建线程方式一: 6 *
创建线程由三种方式:
(1)继承Thread类
(2)实现Runnable接口
(3)实现Callable接口
代码示例:
1、继承Thread类
1 package com.sxt.thread; 2 3 /** 4 * @ClassName StartThread 5 * 创建线程方式一: 6 * 1.创建:继承Thread+重写run 7 * 2.启动:创建子类对象 + start 8 */ 9 public class StartThread extends Thread { 10 11 /** 12 * @Description 线程入口点 13 * @Date 2019-07-23 16:04 14 * @Param [] 15 * @return void 16 **/ 17 @Override 18 public void run() { 19 for (int i = 0;i<20;i++){ 20 System.out.println("一边听歌"); 21 } 22 } 23 24 public static void main(String[] args) { 25 //创建子类对象 26 StartThread st = new StartThread(); 27 //启动 28 st.start();//不保证立即运行,由CPU调用 29 //st.run();//普通方法调用 30 for (int i = 0;i<20;i++){ 31 System.out.println("一边写代码"); 32 } 33 } 34 }
2、实现Runnable接口
1 package com.sxt.thread; 2 3 /** 4 * @ClassName StartThread 5 * 创建线程方式二: 6 * 1.创建:实现Runnable+重写run 7 * 2.启动:创建实现类对象 + Thread对象 + start 8 * 9 * 推荐:避免单继承的局限性,优先使用接口 10 * 方便共享资源 11 */ 12 public class StartRun implements Runnable { 13 14 /** 15 * @Description 线程入口点 16 * @Date 2019-07-23 16:04 17 * @Param [] 18 * @return void 19 **/ 20 @Override 21 public void run() { 22 for (int i = 0;i<20;i++){ 23 System.out.println("一边听歌"); 24 } 25 } 26 27 public static void main(String[] args) { 28 /* 29 //创建实现类对象 30 StartRun sr = new StartRun(); 31 //创建代理类对象 32 Thread t = new Thread(sr); 33 //启动 34 t.start();//不保证立即运行,由CPU调用 35 */ 36 //如果一个对象只是用一次,可以使用匿名的方式创建 37 new Thread(new StartRun()).start(); 38 //st.run();//普通方法调用 39 for (int i = 0;i<20;i++){ 40 System.out.println("一边写代码"); 41 } 42 } 43 }
总结:
示例:
- 创建目标对象: IDownloader id =new IDownloader("图片地址","baidu.png");
- 创建线程对象+关联目标对象: Thread t =new Thread(id);
- 启动线程: t.start()
3、实现Callable接口
1 package com.sxt.thread; 2 3 import java.util.concurrent.*; 4 5 /** 6 * 了解创建线程的方式三:(JUC) 7 * 实现Callable接口,重写call方法 8 * 9 */ 10 public class CDownloader implements Callable<Boolean> { 11 12 private String url;//远程路径 13 private String name;//存储名字 14 15 public CDownloader(String url, String name) { 16 this.url = url; 17 this.name = name; 18 } 19 20 @Override 21 public Boolean call() throws Exception { 22 23 WebDownloader wd = new WebDownloader(); 24 wd.download(url, name); 25 System.out.println("图片名称" + name); 26 return true; 27 } 28 29 public static void main(String[] args) throws ExecutionException, InterruptedException { 30 31 CDownloader cd1 = new CDownloader("https://res.vmallres.com/pimages/detailImg/2019/06/03/20190603145749911985.jpg", "20190603145749911985.jpg"); 32 CDownloader cd2 = new CDownloader("https://res.vmallres.com/pimages/detailImg/2019/06/03/201906031457493181984.jpg", "201906031457493181984.jpg"); 33 CDownloader cd3 = new CDownloader("https://res.vmallres.com/pimages/detailImg/2019/06/03/201906031457505691446.jpg", "201906031457505691446.jpg"); 34 35 //启动3个线程 36 //创建执行服务: 37 ExecutorService ser = Executors.newFixedThreadPool(3); 38 //3.提交执行: 39 Future<Boolean> result1 = ser.submit(cd1); 40 Future<Boolean> result2 = ser.submit(cd2); 41 Future<Boolean> result3 = ser.submit(cd3); 42 //4.获取结果: 43 boolean r1 = result1.get(); 44 boolean r2 = result2.get(); 45 boolean r3 = result3.get(); 46 System.out.println(r3); 47 //5.关闭服务: 48 ser.shutdownNow(); 49 } 50 }