第二篇 多线程(二)
1 package com.zzp.thread; 2 /** 3 * 4 * 静态代理 5 * @author java 6 * 7 */ 8 public class StaticProxy { 9 public static void main(String[] args) { 10 new WedCompany(new You()).happyWedding(); 11 } 12 } 13 14 interface Wedding{ 15 void happyWedding(); 16 } 17 18 class You implements Wedding{ 19 @Override 20 public void happyWedding() { 21 System.out.println("终于结婚了"); 22 } 23 } 24 25 class WedCompany implements Wedding{ 26 private Wedding target; 27 28 public WedCompany(Wedding target) { 29 super(); 30 this.target = target; 31 } 32 33 @Override 34 public void happyWedding() { 35 before(); 36 this.target.happyWedding(); 37 after(); 38 } 39 40 public void before(){ 41 System.out.println("布置狗窝!!!"); 42 } 43 44 public void after(){ 45 System.out.println("闹玉兔!!!"); 46 } 47 }
Lamdba(jdk1.8)新特性
1 package com.zzp.thread; 2 3 public class LambdaTest { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 ILove il = (int a)-> { 8 System.out.println("I like the world"+a); 9 } 10 il.lambda(100); 11 //类型可以省略 12 il = (a)-> { 13 System.out.println("I like the world"+a); 14 } 15 il.lambda(500); 16 //只有一个参数的情况下,括号也可以省略 17 il = a-> { 18 System.out.println("I like the world"+a); 19 } 20 il.lambda(30); 21 22 //如果只有一行代码,花括号也可以省略 23 il = a-> System.out.println("I like the world"+a); 24 il.lambda(11); 25 } 26 27 } 28 29 interface ILove{ 30 void love(int a); 31 } 32 33 class ILike implements ILove{ 34 35 @Override 36 public void love(int a) { 37 System.out.println("I like the world"+a); 38 } 39 40 }
带参数返回值的lambda表达式
1 package com.zzp.thread; 2 /** 3 * 4 * lambda带参数的返回值 5 * @author java 6 * 7 */ 8 public class LambdaTest02 { 9 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 ILove il = (int a,int c)-> { 13 System.out.println("是吗?"); 14 return a+c; 15 } 16 il.lambda(100,200); 17 //类型可以省略 18 il = (a,c)-> { 19 System.out.println("是吗?"); 20 return a+c; 21 } 22 il.lambda(500,1000); 23 24 il = (a,c)-> { 25 return a+c; 26 } 27 il.lambda(30,100); 28 29 //如果只有一行代码,花括号也可以省略 30 il = (a,c)-> a+c; 31 il.lambda(11,12); 32 } 33 34 } 35 36 interface ILove{ 37 int love(int a,int b); 38 } 39 40 class ILike implements ILove{ 41 42 @Override 43 public int love(int a,int c) { 44 System.out.println("是吗?"); 45 return a+c; 46 } 47 48 }
多线程使用lambda表达式
1 package com.zzp.thread; 2 /** 3 * 4 * lambda带参数的返回值 5 * @author java 6 * 7 */ 8 public class LambdaTest03 { 9 10 public static void main(String[] args) { 11 new Thread(()->{ 12 for(int i=0;i<10;i++){ 13 System.out.println("一边学习"); 14 } 15 }).start(); 16 17 new Thread(()-> System.out.println("一边学习");).start(); 18 } 19 20 }