多线程里的Runnable接口创建线程正是用了静态代理模式
package Tread; public class StaticProxy { /** * 静态代理: 真实角色
代理角色 两者要实现相同接口 * * @param args */ public static void main(String[] args) { You you = new You(); WeddingCompany wc = new WeddingCompany(you); wc.marry(); } } interface Marry { public abstract void marry(); } class You implements Marry { @Override public void marry() { System.out.println("你和嫦娥结婚"); } } class WeddingCompany implements Marry { private Marry you; public WeddingCompany() { super(); } public WeddingCompany(You you) { this.you = you; } public void before() { System.out.println("布置猪窝"); } public void after() { System.out.println("闹玉兔"); } @Override public void marry() { before(); you.marry(); after(); } }
示例如下
package Tread; public class Thread03 implements Runnable { @Override public void run() { for (int i = 0; i < 1000; i++) { System.out.println("一边敲代码"); } } } package Tread; public class Thread03App { public static void main(String[] args) { Thread03 t = new Thread03();(真实角色) Thread proxy = new Thread(t); proxy.start(); for (int i = 0; i < 1000; i++) { System.out.println("一边聊qq"); } } }