Thread
创建线程方式1
继承Thread类
子类继承Thread类具备多线程能力
启动线程:子类对象.start()
不建议使用:避免OOP单继承局限性
//创建线程方式1:继承Thread类,重写run()方法,调用start开启线程 //线程开启不一定立即执行,由cpu调度 public class TestThread1 extends Thread{ @Override public void run() { //run方法线程体 for (int i = 0; i < 20; i++) { System.out.println("我在看代码"); } } public static void main(String[] args) { //main线程,主线程 //创建一个线程对象 TestThread1 testThread1 = new TestThread1(); //调用start方法开启线程 testThread1.start(); for (int i = 0; i < 200; i++) { System.out.println("我在学习多线程"); } } }
//要导入commons-io import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; //练习Thread,实现多线程同步下载图片 public class TestThread2 extends Thread{ private String url;//网络图片地址 private String name;//保存的文件名 public TestThread2(String url, String name) { this.url = url; this.name = name; } @Override public void run() {//下载图片线程的执行体 WebDownLoader webDownLoader = new WebDownLoader(); webDownLoader.downLoader(url,name); System.out.println("下载了文件名为:"+name); } public static void main(String[] args) { TestThread2 t1= new TestThread2("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=976577053,714354874&fm=26&gp=0.jpg","1.jpg"); TestThread2 t2 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F05%2F20181005014913_uwpqy.jpg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624010560&t=062a7d0f6d5b657f0bbea623c03cfedb","2.jpg"); TestThread2 t3= new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F6de7e33bf1e0229c2822f33699cc885199dea90f.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624010639&t=97c19ba335a8e64a239509779fa240d0","3.jpg"); t1.start(); t2.start(); t3.start(); } } //下载器 class WebDownLoader{ //下载方法 public void downLoader(String url,String name){ try { FileUtils.copyURLToFile(new URL(url),new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("IO异常,downloader方法出现异常"); } } }
//改成实现runnable接口 import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.net.URL; //练习Thread,实现多线程同步下载图片 public class TestThread2 implements Runnable { private String url;//网络图片地址 private String name;//保存的文件名 public TestThread2(String url, String name) { this.url = url; this.name = name; } @Override public void run() {//下载图片线程的执行体 WebDownLoader webDownLoader = new WebDownLoader(); webDownLoader.downLoader(url,name); System.out.println("下载了文件名为:"+name); } public static void main(String[] args) { TestThread2 t1= new TestThread2("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=976577053,714354874&fm=26&gp=0.jpg","1.jpg"); TestThread2 t2 = new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F05%2F20181005014913_uwpqy.jpg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624010560&t=062a7d0f6d5b657f0bbea623c03cfedb","2.jpg"); TestThread2 t3= new TestThread2("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F6de7e33bf1e0229c2822f33699cc885199dea90f.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1624010639&t=97c19ba335a8e64a239509779fa240d0","3.jpg"); new Thread(t1).start(); new Thread(t2).start(); new Thread(t3).start(); } } //下载器 class WebDownLoader{ //下载方法 public void downLoader(String url,String name){ try { FileUtils.copyURLToFile(new URL(url),new File(name)); } catch (IOException e) { e.printStackTrace(); System.out.println("IO异常,downloader方法出现异常"); } } }
实现runnable接口
实现接口Runnable具有多线程能力
启动线程:传入目标对象+Thread对象.start()
推荐使用:避免单继承局限性,灵活方便,方便同一个对象被多个线程使用
//创建线程方式2:实现runnable接口,重写run方法,执行线程需要丢入runna接口实现类,调用start方法 public class TestThread3 implements Runnable{ @Override public void run() { for (int i = 0; i < 200; i++) { System.out.println("我在看代码"); } } public static void main(String[] args) { //创建runnable接口的实现类对象 TestThread3 testThread3 = new TestThread3(); //创建线程对象,通过线程对象来开启我们的线程,代理 // Thread thread = new Thread(testThread3); // thread.start(); new Thread(testThread3).start(); for (int i = 0; i < 200; i++) { System.out.println("我在学习多线程"); } } }
//模拟龟兔赛跑 public class Race implements Runnable{ private static String winner; @Override public void run() { for (int i = 0; i <=100; i++) { //模拟兔子休息 if(Thread.currentThread().getName().equals("兔子")&&i%10==0){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } //判断比赛是否结束 boolean flag = ganmeOver(i); //如果比赛结束了,就停止程序 if(flag){ break; } System.out.println(Thread.currentThread().getName()+"-->跑了"+i+"步"); } } //判断是否完成比赛 private boolean ganmeOver(int steps){ //判断是否有胜利者 if(winner!=null){//已经存在胜利者了 return true; }{ if(steps>=100){ winner = Thread.currentThread().getName(); System.out.println("winner is"+winner); return true; } } return false; } public static void main(String[] args) { Race race = new Race(); new Thread(race,"兔子").start(); new Thread(race,"乌龟").start(); } }
//静态代理模式总结: //真实对象和代理对象都要实现同一个接口 //代理对象要代理真实角色 //好处:代理对象可以做很多真实对象做不了的事 // 真实对象专注做自己的事 public class StaticProxy { public static void main(String[] args) { WeddingCompany weddingCompany = new WeddingCompany(new You()); weddingCompany.HappyMarry(); } } interface Marry{ void HappyMarry(); } //真实角色,你去结婚 class You implements Marry{ @Override public void HappyMarry() { System.out.println("要结婚了,超开心"); } } //代理角色,帮助你结婚 class WeddingCompany implements Marry{ private Marry target; public WeddingCompany(Marry target) { this.target = target; } @Override public void HappyMarry() { before(); this.target.HappyMarry(); after(); } private void after() { System.out.println("结婚之后收尾款"); } private void before() { System.out.println("结婚前布置现场"); } }
//推导lambda表达式 public class TestLambda1 { //静态内部类 static class Like2 implements ILike{ @Override public void lambda() { System.out.println("i like lambda2"); } } public static void main(String[] args) { ILike like = new Like(); like.lambda(); like = new Like2(); like.lambda(); //局部内部类 class Like3 implements ILike{ @Override public void lambda() { System.out.println("i like lambda3"); } } like = new Like3(); like.lambda(); //匿名内部类,没有类的名称,必须借助接口或者父类 like = new ILike() { @Override public void lambda() { System.out.println("i like lambda4"); } }; like.lambda(); //用lambda简化 like = () ->{ System.out.println("i like lambda5"); }; like.lambda(); } } //定义一个函数是接口 interface ILike{ void lambda(); } //实现类 class Like implements ILike{ @Override public void lambda() { System.out.println("i like lambda"); } }
public class TestLambda2 { public static void main(String[] args) { //lambda表达式简化 ILove love =(int a)-> { System.out.println("I love you-->"+a); }; //简化1.去掉参数值的类型 love = (a)-> { System.out.println("I love you-->"+a); }; //简化2.简化括号 love = a-> { System.out.println("I love you-->"+a); }; //简化3.去掉花括号 love = a->System.out.println("I love you-->"+a); //总结: //lambda表达式只能有一行代码的情况下才能简化成一行,如果有多行,那么就用代码块包裹 //前提是接口为函数式接口 //多个参数也可以去掉参数类型,要去掉就都去掉,必须加上括号 love.love(2); } } interface ILove{ void love(int a); }