多线程详解_1
p1-p10的内容
1: 多线程,进程,多任务
2:创建线程的三种方式
2.1:继承Thread类
1 package Thread; 2 3 /** 4 * author liulei 5 * data 5.23 6 * since 1.8 7 * version 1.0 8 * Description 继承Thread实现线程 9 */ 10 public class testThread { 11 public static void main(String[] args) { 12 T t = new T(); 13 t.start();//start方法启动线程 14 } 15 } 16 class T extends Thread{//1 继承Thread 17 public void run(){//2 重写run方法 18 System.out.println("----hahah----"); 19 } 20 21 }
2.2:实现Runnable接口
程序用到的Jar包文件https://files.cnblogs.com/files/henuliulei/commons-io-1.4.zip
多线程下载文件
1 package Thread; 2 3 import org.apache.commons.io.FileUtils; 4 5 import java.io.File; 6 import java.io.IOException; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 10 /** 11 * author liulei 12 * data 5.23 13 * since 1.8 14 * version 1.0 15 * Description 实现Runnable接口 16 */ 17 public class Test2 implements Runnable{ 18 19 20 @Override 21 public void run() { 22 WebDownloader down = new WebDownloader(); 23 try { 24 down.downloader("https://csdnimg.cn/medal/github@240.png","a.png"); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 30 public static void main(String[] args) { 31 Test2 test2 =new Test2(); 32 new Thread(test2).start(); 33 } 34 } 35 class WebDownloader 36 { 37 public void downloader(String url,String name) throws IOException { 38 FileUtils.copyURLToFile(new URL(url),new File(name)); 39 } 40 }
多线程买票演示
1 package Thread; 2 3 /** 4 * author liulei 5 * data 5.23 6 * since 1.8 7 * version 1.0 8 * Description 买票演示 9 */ 10 public class Test3 implements Runnable{ 11 private int ticketNums = 10; 12 13 @Override 14 public void run() { 15 while(true){ 16 if(ticketNums <= 0){ 17 break; 18 } 19 System.out.println(Thread.currentThread().getName()+"--->拿到了"+ticketNums--+"票"); 20 } 21 } 22 23 public static void main(String[] args) { 24 Test3 test3 = new Test3(); 25 new Thread(test3,"线程一").start(); 26 new Thread(test3,"线程二").start(); 27 new Thread(test3,"线程三").start(); 28 } 29 }
多线程演示龟兔赛跑
1 package Thread; 2 3 /** 4 * author liulei 5 * data 6 * since 1.8 7 * version 1.0 8 * Description 龟兔赛跑 9 */ 10 public class Game implements Runnable{ 11 private String winner = ""; 12 private Boolean gameover(int steps){ 13 if(winner != ""){ 14 return true; 15 } 16 if(steps >= 100){ 17 winner = Thread.currentThread().getName(); 18 System.out.println("winner is " + winner); 19 return true; 20 } 21 return false; 22 } 23 @Override 24 public void run() { 25 for(int i = 0;i <= 100; i++){ 26 boolean flag = gameover(i); 27 if(flag){ 28 break; 29 } 30 if(Thread.currentThread().getName().equals("兔子")){ 31 try { 32 Thread.sleep(1); 33 } catch (InterruptedException e) { 34 e.printStackTrace(); 35 } 36 } 37 System.out.println(Thread.currentThread().getName() + "跑了"+ i + "步"); 38 } 39 } 40 41 public static void main(String[] args) { 42 Game a = new Game(); 43 new Thread(a,"兔子").start(); 44 new Thread(a,"乌龟").start(); 45 } 46 }
2.3:实现Callable接口
1 package Thread; 2 3 import org.apache.commons.io.FileUtils; 4 5 import java.io.File; 6 import java.io.IOException; 7 import java.net.URL; 8 import java.util.concurrent.*; 9 10 /** 11 * author liulei 12 * data 5.23 13 * since 1.8 14 * version 1.0 15 * Description 实现Callable 接口 16 */ 17 public class Test4 implements Callable<Boolean> { 18 String url;//图片路径 19 String name;//文件名 20 21 public Test4(String url, String name) { 22 this.url = url; 23 this.name = name; 24 } 25 26 @Override 27 public Boolean call() throws Exception { 28 WebDownloader down = new WebDownloader(); 29 down.downloader(url,name); 30 System.out.println("下载成功"); 31 return true; 32 } 33 34 public static void main(String[] args) throws ExecutionException, InterruptedException { 35 Test4 t = new Test4("https://csdnimg.cn/medal/github@240.png","aa.png"); 36 //创建执行服务 37 ExecutorService ser = Executors.newFixedThreadPool(1); 38 //提交执行 39 Future<Boolean> submit = ser.submit(t); 40 //获取结果 41 Boolean aBoolean = submit.get(); 42 System.out.println(aBoolean); 43 //关闭服务 44 ser.shutdown(); 45 } 46 }
3:静态代理
1 package Thread; 2 3 /** 4 * author liulei 5 * data 5.23 6 * since 1.8 7 * version 1.0 8 * Description 静态代理 9 */ 10 public class Test5 { 11 /* 12 真实对象和代理对象是实线同一个接口 13 代理对象要代理真实角色 14 好处:代理角色可以做很多真实角色做不了的事情 15 真实对象专注做自己的事情 16 */ 17 public static void main(String[] args) { 18 weddingCompany webcop = new weddingCompany(new you()); 19 new Thread(()-> System.out.println("iloveyou")).start(); 20 webcop.HappyMarry(); 21 } 22 23 } 24 interface Marry{ 25 void HappyMarry(); 26 } 27 class you implements Marry{//真实角色 28 29 @Override 30 public void HappyMarry() { 31 System.out.println("我要结婚"); 32 } 33 } 34 class weddingCompany implements Marry{//代理角色 35 private Marry target; 36 37 public weddingCompany(Marry target) { 38 this.target = target; 39 } 40 @Override 41 public void HappyMarry() { 42 before(); 43 this.target.HappyMarry(); 44 after(); 45 } 46 public void before(){ 47 System.out.println("结婚前,婚礼公司帮助我布置婚礼"); 48 49 } 50 public void after(){ 51 System.out.println("结婚后,收费用"); 52 } 53 }
4:lambda表达式
1 package Thread; 2 3 /** 4 * author liulei 5 * data 6 * since 1.8 7 * version 1.0 8 * Description lambda表达式 9 */ 10 public class Test7 { 11 /* 12 lambda表达式只能有一行代码的情况下才能简化为一行去掉大括号,否则只能用代码块包裹 13 前提是接口为函数式接口 14 多个参数也可以去掉参数类型,要去掉都去掉,必须加括号 15 */ 16 public class Like1 implements ILike{ 17 18 @Override 19 public void lab() { 20 System.out.println("成员内部类"); 21 } 22 } 23 public static void main(String[] args) { 24 25 class Like2 implements ILike{ 26 27 @Override 28 public void lab() { 29 System.out.println("局部内部类"); 30 } 31 } 32 ILike Like3 = null; 33 Like3 = ()-> System.out.println("lambda表达式"); 34 Like3.lab();//当然有参数也可以加入参数 35 } 36 } 37 //定义一个函数式接口 38 interface ILike{ 39 void lab(); 40 } 41 //实现类 42 class Like implements ILike{ 43 44 @Override 45 public void lab() { 46 System.out.println("实现类"); 47 } 48 49 }
作者:你的雷哥
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。