`
anoxic
  • 浏览: 1528 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

java中简单的线程和委托事例

    博客分类:
  • java
阅读更多
一、线程池的应用:

public class TestThread implements Runnable    {
private String name;

public TestThread(String name) {
  this.name = name;
}
@Override
public void run() {
  try {
   Thread.sleep(1000 * 3);
  } catch (Exception ex) {
  }
  System.out.println("正在执行:" + this.name);
}
}



import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;



public class ThreadPool {

public static void main(String[] args) {
   
  // 创建一个可重用固定线程数的线程池
  ExecutorService pool = Executors.newFixedThreadPool(1);

  // 创建实现了Runnable接口对象,Thread对象当然也实现了Runnable接口
for (int i = 0; i < 20; i++) {
  
  TestThread t1 = new TestThread(i + "");
  // 将线程放入池中进行执行
pool.execute(t1);
}

  // 关闭线程池
  pool.shutdown();

}
}





二、线程的应用:



public class ThreadDemo{

public static void main(String[] args) {


new Thread(){
    @Override
    public void run() {
   for (int i = 0; i < 20; i++) {

System.out.println("正在执行:" + i);

  }

    }
   }.start();

}}



三、委托的应用:



import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public abstract class Delegator implements InvocationHandler {

protected Object obj_orgin = null; // 原始对象
protected Object obj_proxy = null; // 代理对象


public Delegator() {
}

public Delegator(Object orgin) {
  this.createProxy(orgin);
}

/*
  * 完成原始对象和委托对象的实例化
  *
  * @param orgin 原始对象实例
  */

protected Object createProxy(Object orgin) {
  obj_orgin = orgin;
  // 下面语句中orgin.getClass().getClassLoader()为加载器,orgin.getClass().getInterfaces()为接口集
  obj_proxy = Proxy.newProxyInstance(orgin.getClass().getClassLoader(), orgin.getClass().getInterfaces(), this); // 委托
  return obj_proxy;
}

/*
  * 对带有指定参数的指定对象调用由此 Method 对象表示的底层方法,具体请参见Java API
  *
  * @param args 参数
  *
  * @param method 方法类实例
  */

protected Object invokeSuper(Method method, Object[] args) throws Throwable {
  return method.invoke(obj_orgin, args);
}

// --------------实现InvocationHandler接口,要求覆盖------------
// 下面实现的方法是当委托的类调用toString()方法时,操作其他方法而不是该类默认的toString(),这个类的其他方法则不会。

public Object invoke(Object obj, Method method, Object[] args)
   throws Throwable {
  // 缺省实现:委托给obj_orgin完成对应的操作
  if (method.getName().equals("toString")) { // 对其做额外处理
   return this.invokeSuper(method, args) + "$Proxy";
  } else { // 注意,调用原始对象的方法,而不是代理的(obj==obj_proxy)
   return this.invokeSuper(method, args);
  }
}

}



import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Map;


@SuppressWarnings("rawtypes")
public class Delegator4Map extends Delegator {
//private static Log _log = LoggerFactory.getLog(Delegator4Map.class);
private Map orginClass = null; // 原始对象
private Map proxyClass = null; // 代理对象

public Map getOrgin() {
  return orginClass;
}

public Map getProxy() {
  return proxyClass;
}

public Delegator4Map(Map orgin) {
  super(orgin);
  orginClass = orgin;
  proxyClass = (Map) super.obj_proxy;
}

public Object invoke(Object obj, Method method, Object[] args)
   throws Throwable {
  if (method.getName().equals("size")) { // 修改size处理逻辑
   Object res2 = new Integer(-1);
   System.out.println("调用委托的方法");
   return res2;
  } else {
   System.out.println("调用原始的方法");
   return super.invoke(obj, method, args);
  }
}

public static void main(String[] args) throws IOException {
  Delegator4Map rtm = new Delegator4Map(new Hashtable());
  Map m = rtm.getProxy();
  m.size();
}

}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics