您的位置:首页 > 职场人生

黑马程序员 高新技术 内省、动态代理、线程池和类加载器

2014-09-17 11:50 387 查看
------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

内省

1什么是内省?

内省(Introspector)是Java语言对bean类属性,事件的一种缺省处理方法。例如类A中有属性name,那我们可以通过getName,setname来得到其值或设置新的值。通过getName/setName来访问name 属性,这就是默认的规则。Java中提供了一套API用某个属来访问getter/setter方法,这些API存放于包java.beans中。

2直接通过属性的描述器java.beans.PropertyDescriptor类,来访问属性的getter/setter 方法;

<span style="font-size:18px;">importjava.beans.PropertyDescriptor;
importjava.lang.reflect.Method;

public class Reflect{

public static voidmain(String[] args)throws Exception{
Point point=new Point(1,2);
String proName="x";
getProperty(point,proName);
setProperty(point,proName);
}
private static voidsetProperty(Point point,String proName)throws Exception{

PropertyDescriptor proDescriptor=new
PropertyDescriptor(proName,Point.class);
MethodmethodSetX=proDescriptor.getWriteMethod();
methodSetX.invoke(point,8);
System.out.println(point.getX());
}
private static voidgetProperty(Point point,String proName)throws Exception{

PropertyDescriptor proDescriptor=new
PropertyDescriptor(proName,Point.class);
MethodmethodGetX=proDescriptor.getReadMethod();
System.out.println(methodGetX.invoke(point));

}
}
public class Point {

private Integer x;
private Integer y;
public Point(Integerx,Integer y){
super();
this.x=x;
this.y=y;
}
public IntegergetX(){
return x;
}
public voidsetX(Integer x){
this.x=x;
}
public IntegergetY(){
return y;
}
public voidsetY(Integer y){
this.y=y;
}</span>

二.动态代理

一、什么是动态代理?

动态代理是为对象提供一种代理以控制对这个对象的访问。

二、什么时候使用动态代理?

 当不允许直接访问某些类时或对访问的对象要做特殊处理时。

三、Java动态代理类位于Java.lang.reflect包下,一般主要涉及到以下两个类:

(1). InterfaceInvocationHandler:该接口中仅定义了一个方法Object:invoke(Object obj,Method method, Object[] args)。在实际使用时,第一个参数obj一般是指代理类,method是被代理的方法,如上例中的request(),args为该方法的参数数组。这个抽象方法在代理类中动态实现。

 

(2).Proxy:该类即为动态代理类,作用类似于上例中的ProxySubject,其中主要包含以下内容:

ProtectedProxy(InvocationHandler h):构造函数,估计用于给内部的h赋值。

Static ClassgetProxyClass (ClassLoader loader, Class[] interfaces):获得一个代理类,其中loader是类装载器,interfaces是真实类所拥有的全部接口的数组。

Static ObjectnewProxyInstance(ClassLoader loader,

Class[] interfaces,InvocationHandler h);

 

返回代理类的一个实例,返回后的代理类可以当作被代理类使用(可使用被代理类的在Subject接口中声明过的方法)。

 

动态代理实例:

import java.lang.reflect.*;
class ProxyDemo
{
public static void main(String[] args) throws Exception

{

UserServiceBean ub=new UserServiceBean();

UserViceProxy up=new UserViceProxy(ub);

UserService us=(UserService)Proxy.newProxyInstance(ub.getClass().

getClassLoader();

ub.getClass().getInterfaces(),up);

us.say();
}
}
interface UserService //定义一个接口
{
public void say();
}
class UserServiceBean implements UserService //实现UserService接口
{
public void say();
{

System.out.println("Hello!");

}
}
class UserViceProxy implements InvocationHandler
{
private Object obj;

UserViceProxy(Object obj)

{

this.obj=obj;

}
public Object invoke(Object proxy,Method method,Object[] args)throws Throwable

{

return method.invoke(obj,args);

}
}

三、线程池

线程池的作用:

线程池作用就是限制系统中执行线程的数量。

 

 

1.减少了创建和销毁线程的次数,每个工作线程都可以被重复利用,可执行多个任务。

2.可以根据系统的承受能力,调整线程池中工作线线程的数目,防止因为消耗过多的内存,而把服务器累趴下(每个线程需要大约1MB内存,线程开的越多,消耗的内存也就越大,最后死机)。

Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具。真正的线程池接口是ExecutorService。

比较重要的几个类:

ExecutorService

真正的线程池接口。

ScheduledExecutorService

能和Timer/TimerTask类似,解决那些需要任务重复执行的问题。

ThreadPoolExecutor

ExecutorService的默认实现。

ScheduledThreadPoolExecutor

继承ThreadPoolExecutor的ScheduledExecutorService接口实现,周期性任务调度的类实现。

要配置一个线程池是比较复杂的,尤其是对于线程池的原理不是很清楚的情况下,很有可能配置的线程池不是较优的,因此在Executors类里面提供了一些静态工厂,生成一些常用的线程池。

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

public class NewSingleThreadExecutor {

public static void main(String[] args) {
// TODO Auto-generated method stub

//创建一个可重用固定线程数的线程池

ExecutorService pool=Executors.newSingleThreadExecutor();
//创建实现了Runnable接口对象
Thread t1=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程1");
}
};

Thread t2=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程2");
}
};

Thread t3=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程3");
}
};

Thread t4=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程4");
}
};

Thread t5=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程5");
}
};

//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//关闭线程
pool.shutdown();
}
}

输出结果
pool-1-thread-1正在执行线程1
pool-1-thread-1正在执行线程2
pool-1-thread-1正在执行线程3
pool-1-thread-1正在执行线程4
pool-1-thread-1正在执行线程5

2.newFixedThreadPool

<span style="font-size:18px;">package threadPool;

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

public class NexFixedThreadPool {

public static void main(String[] args) {
// TODO Auto-generated method stub

//创建一个可重用固定线程数的线程池

ExecutorService pool=Executors.newFixedThreadPool(3);
//创建实现了Runnable接口对象
Thread t1=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程1");
}
};

Thread t2=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程2");
}
};

Thread t3=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程3");
}
};

Thread t4=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程4");
}
};

Thread t5=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程5");
}
};

//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//关闭线程
pool.shutdown();
}
}

输出结果
pool-1-thread-3正在执行线程3
pool-1-thread-1正在执行线程1
pool-1-thread-2正在执行线程2
pool-1-thread-1正在执行线程5
pool-1-thread-3正在执行线程4
</span>


3 newCachedThreadPool

import java.util.concurrent.Executors;

public class NewCachedThreadPool {

public static void main(String[] args) {
// TODO Auto-generated method stub

//创建一个可重用固定线程数的线程池

ExecutorService pool=Executors.newCachedThreadPool();
//创建实现了Runnable接口对象
Thread t1=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程1");
}
};

Thread t2=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程2");
}
};
Thread t3=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程3");
}
};
Thread t4=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程4");
}
};
Thread t5=new Thread(){
public void run(){
System.out.println(Thread.currentThread().getName()+"正在执行线程5");
}
};
//将线程放入池中进行执行
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
//关闭线程
pool.shutdown();
}
}

输出结果:
pool-1-thread-1正在执行线程1
pool-1-thread-4正在执行线程4
pool-1-thread-5正在执行线程5
pool-1-thread-3正在执行线程3
pool-1-thread-2正在执行线程2

4ScheduledThreadPool

pool-1-thread-4boombing
pool-1-thread-4boombing package threadPool;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.ScheduledThreadPoolExecutor;
public class ScheduledThreadPoolExecutorTest {

public static void main(String[] args) {
// TODO Auto-generated method stub

ScheduledThreadPoolExecutor stpe=new ScheduledThreadPoolExecutor(5);
stpe.scheduleAtFixedRate(new Runnable(){
public void run(){
System.out.println(Thread.currentThread().getName()+"boombing");
}
},1000,1000, TimeUnit.MILLISECONDS);//1s后执行1个线程,然后以1+1=2s执行1个线程,然后1+2*1=3s执行1个线程

stpe.scheduleAtFixedRate(new Runnable(){
public void run(){
System.out.println(System.nanoTime());// 返回最准确的可用系统计时器的当前值
}
},1000,2000, TimeUnit.MILLISECONDS);
}
}

输出结果
pool-1-thread-1boombing
35676046677778
pool-1-thread-2boombing
pool-1-thread-1boombing
35678045981107
pool-1-thread-1boombing
pool-1-thread-1boombing
35680046431459

35682046069371


四、类加载器

1、什么是类加载器?

 加载类的工具。

2、为什么要创建类加载器?

既然Java虚拟金已经有了类加载器,我们还要自己创建其他的呢?

默认的类加载器只知道如何从本地系统加载类。当你的程序完全在本机编译的话,默认的类加载器一般都工作的很好。但是Java很容易的从网络上而不只是本地加载类。举个例子,浏览器可以通过自定义的类加载器加载类。还有很多加载类的方式。除了简单的从本地或者网络外,还可以通过自定义Java中的地方之一:

执行非信任代码前自动验证数字签名

根据用户提供的密码解密代码

根据用户的需要动态的创建类

3、java类加载器

Java虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:BootStrap,ExtClassLoader,AppClassLoader

类加载--------------------------------------------管辖范围

BootStrap--------------------------------JRE/lip/rt.jar

ExtClassLoader--------------------------JRE/lib/ext/*jar

AppClassCoader---------------------------ClassPath指定的所有jar目录下

MyClassLoader ItcassLoader--------------------------程序指定的特殊目录

的原因。

4、怎么定义一个类加载器?

1.自定义的类加载器继承ClassLoader

2.覆盖findClass方法

3.调用defineClass方法把得到的class文件转换成字节码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: