/concurrent/waitnotify/doublebuffer/Main.java |
concurrent, waitnotify |
|
public class Main {
public static void main(String[] args) {
Factory f = new Factory();
f.start();
Kid k = new Kid();
k.start();
new DoubleBufferList(Tools.lP,Tools.lT,1).check();
}
}
|
/concurrent/waitnotify/doublebuffer/DoubleBufferList.java |
concurrent, waitnotify |
|
public class DoubleBufferList {
private List<Object> lP;
private List<Object> lT;
private int gap;
public DoubleBufferList(List lP, List lT, int gap) {
this.lP = lP;
this.lT = lT;
this.gap = gap;
}
public void check() {
Runnable runner = new Runnable() {
public void run() {
while (true) {
if (lT.size() == 0) {
synchronized (lT) {
synchronized (lP) {
lT.addAll(lP);
lP.notifyAll();
}
lP.clear();
}
}
try {
Thread.sleep(gap);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread = new Thread(runner);
thread.start();
}
}
|
/concurrent/waitnotify/doublebuffer/Kid.java |
concurrent, waitnotify |
|
public class Kid extends Thread {
long time1 = System.currentTimeMillis();
int count = 0;
public void run() {
while (true) {
synchronized (Tools.lT) {
if (Tools.lT.size() != 0){
Tools.lT.remove(0);
count++;
}
}
if (count == 100000) {
System.out.println("time:" + (System.currentTimeMillis() - time1));
System.exit(0);
}
}
}
}
|
/concurrent/waitnotify/doublebuffer/Factory.java |
concurrent, waitnotify |
|
public class Factory extends Thread {
public void run() {
while (true) {
Toy t = new Toy();
t.setName("玩具");
synchronized (Tools.lP) {
if (Tools.lP.size() >= 2000) {
try {
Tools.lP.wait();
} catch (Exception e) {
// TODO: handle exception
}
}
Tools.lP.add(t);
System.out.println("put one");
}
}
}
}
|
/concurrent/waitnotify/doublebuffer/Toy.java |
concurrent, waitnotify |
|
public class Toy {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
|
/concurrent/waitnotify/doublelock/Main.java |
concurrent, waitnotify |
|
public class Client {
public static void main(String[] args){
Object producerMonitor = new Object();
Object consumerMonitor = new Object();
Container<Bread> container = new Container<Bread>(10);
//生产者开动
new Thread(new Producer(producerMonitor,consumerMonitor,container)).start();
new Thread(new Producer(producerMonitor,consumerMonitor,container)).start();
new Thread(new Producer(producerMonitor,consumerMonitor,container)).start();
new Thread(new Producer(producerMonitor,consumerMonitor,container)).start();
//消费者开动
new Thread(new Consumer(producerMonitor,consumerMonitor,container)).start();
new Thread(new Consumer(producerMonitor,consumerMonitor,container)).start();
}
}
|
/concurrent/waitnotify/doublelock/Consumer.java |
concurrent, waitnotify |
|
public class Consumer implements Runnable{
//简单的模拟,这里一个生产容器,设置成final类型的话不允许再次赋值
private final Container<Bread> container;
//生产者线程监听器
private final Object producerMonitor;
//消费者线程监听器
private final Object consumerMonitor;
public Consumer(Object producerMonitor,Object consumerMonitor,Container<Bread> container){
this.producerMonitor = producerMonitor;
this.consumerMonitor = consumerMonitor;
this.container = container;
}
@Override
public void run() {
while(true){
consume();
}
}
//消费,两把锁的问题
public void consume(){
//如果发现容器已经满了,生产者要停
if(container.isEmpty()){
//唤醒生产者
synchronized(producerMonitor){
if(container.isEmpty()){
producerMonitor.notify();
}
}
//消费者挂起
synchronized(consumerMonitor){
try {
if(container.isEmpty()){
System.out.println("消费者挂起。。。");
consumerMonitor.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}else{
//还有面包可以进行消费
Bread bread = container.get();
System.out.println("bread:"+bread);
}
}
}
|
/concurrent/waitnotify/doublelock/Producer.java |
concurrent, waitnotify |
|
public class Producer implements Runnable {
//简单的模拟,这里一个生产容器,设置成final类型的话不允许再次赋值
private final Container<Bread> container;
//生产者线程监听器
private final Object producerMonitor;
//消费者线程监听器
private final Object consumerMonitor;
public Producer(Object producerMonitor,Object consumerMonitor,Container<Bread> container){
this.producerMonitor = producerMonitor;
this.consumerMonitor = consumerMonitor;
this.container = container;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
while(true){
produce();
}
}
public void produce(){
//这里为了形象,模拟几个制作面包的步骤
step1();
Bread bread = step2();
//如果发现容器已经满了,生产者要停
if(container.isFull()){
//唤醒消费者
synchronized(consumerMonitor){
if(container.isFull()){
consumerMonitor.notify();
}
}
//生产者挂起,两把锁的问题
synchronized(producerMonitor){
try {
if(container.isFull()){
System.out.println("生产者挂起...");
producerMonitor.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}else{
//容器中还有容量,把面包放到容器内,这里可能会有丢失
boolean result = container.add(bread);
System.out.println("Producer:"+result);
}
}
public void step1(){}
public Bread step2(){
return new Bread();
}
}
|
/concurrent/waitnotify/doublelock/Container.java |
concurrent, waitnotify |
|
public class Container<T> {
private final int capacity;
private final List<T> list;
public Container(int capacity){
this.capacity = capacity;
list = new ArrayList<T>(capacity);
}
public List<T> getList(){
return list;
}
/**
* 添加产品
* @param product
*/
public synchronized boolean add(T product){
if(list.size()<capacity){
list.add(product);
return true;
}
return false;
}
/**
* 满
* @return
*/
public synchronized boolean isFull(){
if(list.size()>=capacity){
return true;
}
return false;
}
public synchronized boolean isEmpty(){
return list.isEmpty();
}
public synchronized T get(){
if(list.size()>0){
return list.remove(0);
}
return null;
}
public synchronized int getSize(){
return list.size();
}
public int getCapacity(){
return capacity;
}
}
|
/AccountResponse.xml |
|
|
<?xml version="1.0" encoding="ISO8859-1" ?>
<wsdl:definitions targetNamespace=http://aaaserver.soap.isimanager.huawei.com
xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://aaaserver.soap.isimanager.huawei.com" xmlns:intf="http://aaaserver.soap.isimanager.huawei.com"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://aaadata.aaaserver.soap.isimanager.huawei.com"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<schema targetNamespace=http://aaadata.soap.isimanager.huawei.com
xmlns="http://www.w3.org/2001/XMLSchema">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<complexType name="AccountResponse">
<sequence>
<element name="transid" type="xsd:string" />
<element name="result" type="xsd:int" />
<element name="description" nillable="true" type="xsd:string" />
</sequence>
</complexType>
</schema>
</wsdl:types>
<wsdl:message name="getAccountResponseResponse">
<wsdl:part name="getAccountResponseReturn" type="tns1:AccountResponse" />
</wsdl:message>
<wsdl:message name="getAccountResponseRequest">
<wsdl:part name="req" type="tns1:AccountRequest" />
</wsdl:message>
<wsdl:portType name="AAAIF">
<wsdl:operation name="getAccountResponse"
parameterOrder="req">
<wsdl:input message="impl:getAccountResponseRequest" name="getAccountResponseRequest" />
<wsdl:output message="impl:getAccountResponseResponse"
name="getAccountResponseResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AAAIFSoapBinding" type="impl:AAAIF">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getAccountResponse">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="getAccountResponseRequest">
<wsdlsoap:body encodingStyle=http://schemas.xmlsoap.org/soap/encoding /
namespace="http://aaaserver.soap.isimanager.huawei.com" use="encoded" />
</wsdl:input>
<wsdl:output name="getAccountResponseResponse">
<wsdlsoap:body encodingStyle=http://schemas.xmlsoap.org/soap/encoding /
namespace="http://aaaserver.soap.isimanager.huawei.com" use="encoded" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AAAIFService">
<wsdl:port binding="impl:AAAIFSoapBinding" name="AAAIF">
<wsdlsoap:address location="http://localhost:8080/sms/services/AAAIF" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
|
/AccountRequest.xml |
|
|
<?xml version="1.0" encoding="ISO8859-1" ?>
<wsdl:definitions targetNamespace=http://aaaserver.soap.isimanager.huawei.com
xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://aaaserver.soap.isimanager.huawei.com" xmlns:intf="http://aaaserver.soap.isimanager.huawei.com"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="http://aaadata.aaaserver.soap.isimanager.huawei.com"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<schema targetNamespace=http://aaadata.soap.isimanager.huawei.com
xmlns="http://www.w3.org/2001/XMLSchema">
<import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<complexType name="AccountRequest">
<sequence>
<element name="nodecode" type="xsd:string" />
<element name="sign" type="xsd:string" />
<element name="transid" type="xsd:string" />
<element name="status" type="xsd:int" />
<element name="ipaddr" type="xsd:string" />
<element name="accounttype" type="xsd:int" />
<element name="accountinfo" type="xsd:string" />
<element name="begintime" nillable="true" type="xsd:string" />
<element name="endtime" nillable="true" type="xsd:string" />
<element name="limitflux" type="xsd:int" />
<element name="flux" type="xsd:int" />
<element name="limitdur" type="xsd:int" />
<element name="duration" type="xsd:int" />
</sequence>
</complexType>
</schema>
</wsdl:types>
<wsdl:message name="getAccountResponseResponse">
<wsdl:part name="getAccountResponseReturn" type="tns1:AccountResponse" />
</wsdl:message>
<wsdl:message name="getAccountResponseRequest">
<wsdl:part name="req" type="tns1:AccountRequest" />
</wsdl:message>
<wsdl:portType name="AAAIF">
<wsdl:operation name="getAccountResponse"
parameterOrder="req">
<wsdl:input message="impl:getAccountResponseRequest" name="getAccountResponseRequest" />
<wsdl:output message="impl:getAccountResponseResponse"
name="getAccountResponseResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AAAIFSoapBinding" type="impl:AAAIF">
<wsdlsoap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getAccountResponse">
<wsdlsoap:operation soapAction="" />
<wsdl:input name="getAccountResponseRequest">
<wsdlsoap:body encodingStyle=http://schemas.xmlsoap.org/soap/encoding /
namespace="http://aaaserver.soap.isimanager.huawei.com" use="encoded" />
</wsdl:input>
<wsdl:output name="getAccountResponseResponse">
<wsdlsoap:body encodingStyle=http://schemas.xmlsoap.org/soap/encoding /
namespace="http://aaaserver.soap.isimanager.huawei.com" use="encoded" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AAAIFService">
<wsdl:port binding="impl:AAAIFSoapBinding" name="AAAIF">
<wsdlsoap:address location="http://localhost:8080/sms/services/AAAIF" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
|
/spring/simulator/LeamClassPathXMLApplicationContext.java |
spring |
|
package spring.simulator;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.XPath;
public class LeamClassPathXMLApplicationContext
{
//BeanDefinition类型的List
private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
//容器的主要数据结构beanDefinitionMap
private Map<String, Object> beanDefinitionMap = new HashMap<String, Object>();
public LeamClassPathXMLApplicationContext(String fileName) {
readXml(fileName);
initBeans();
injectProperties();
}
/**
* 根据id从容器中获取bean
* <一句话功能简述>
* <功能详细描述>
* @param id
* @return
* @see [类、类#方法、类#成员]
*/
public Object getBean(String id) {
return beanDefinitionMap.get(id);
}
/**
* 读取xml文件信息进行解析,并将bean节点加入beanDefines中
* <一句话功能简述>
* <功能详细描述>
* @param fileName
* @see [类、类#方法、类#成员]
*/
private void readXml(String fileName) {
SAXReader reader = new SAXReader();
Document document = null;
try
{
URL xmlPath = this.getClass().getClassLoader().getResource(fileName);
document = reader.read(xmlPath);
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans"); //加入命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean"); //创建beans/bean查询路径
xsub.setNamespaceURIs(nsMap); //设置命名空间
List<Element> beans = xsub.selectNodes(document); //获取文档下所有bean节点
for (Element element : beans) {
String id = element.attributeValue("id"); //获取id属性值
String clazz = element.attributeValue("class"); //获取class属性值
BeanDefinition beanDefinition = new BeanDefinition(id, clazz);
XPath propertysub = element.createXPath("ns:property");
propertysub.setNamespaceURIs(nsMap); //设置命名空间
List<Element> properties = propertysub.selectNodes(element); //获取bean下所有property节点
for (Element property : properties) {
String propertyName = property.attributeValue("name");
String propertyRef = property.attributeValue("ref"); //元素内部引用的属性也获取
PropsDefinition propsDefinition = new PropsDefinition(propertyName, propertyRef);
beanDefinition.getProperties().add(propsDefinition);
}
beanDefinitions.add(beanDefinition);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 遍历beanDefines将bean加入map中
* <一句话功能简述>
* <功能详细描述>
* @see [类、类#方法、类#成员]
*/
private void initBeans() {
for (BeanDefinition beanDefinition : beanDefinitions) {
if (null != beanDefinition.getClassName() && !"".equals(beanDefinition.getClassName().trim())) {
try
{
beanDefinitionMap.put(beanDefinition.getId(),
Class.forName(beanDefinition.getClassName()).newInstance());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
/**
* 根据set方法实现依赖注入
* 遍历beanDefines集合,对每个beanDefinition,实现property节点的注入功能
* <一句话功能简述>
* <功能详细描述>
* @see [类、类#方法、类#成员]
*/
private void injectProperties() {
for (BeanDefinition beanDefinition : beanDefinitions) {
Object bean = beanDefinitionMap.get(beanDefinition.getId());
if (null != bean) {
try
{
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for (PropsDefinition propertyDefinition : beanDefinition.getProperties()) {
for (PropertyDescriptor propertyDesc : ps) {
if (propertyDefinition.getName().equals(propertyDesc.getName())) {
Method setter = propertyDesc.getWriteMethod(); //获取属性的setter方法
if (null != setter) {
Object value = beanDefinitionMap.get(propertyDefinition.getRef());
setter.setAccessible(true);
setter.invoke(bean, value);
}
break;
}
}
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
|
/spring/simulator/PropsDefinition.java |
spring |
|
package spring.simulator;
public class PropsDefinition
{
private String name; //name属性
private String ref; //ref属性
public PropsDefinition(String name, String ref) {
this.name = name;
this.ref = ref;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
}
|
/spring/simulator/BeanDefinition.java |
spring |
|
package spring.simulator;
import java.util.ArrayList;
import java.util.List;
/**
* BeanDefinition 的定义类
* <一句话功能简述>
* <功能详细描述>
*/
public class BeanDefinition
{
private String id; //bean id
private String className; //bean className
private List<PropsDefinition> properties = new ArrayList<PropsDefinition>(); //定义bean中property子节点
public BeanDefinition(String id, String className) {
this.id = id;
this.className = className;
}
/**
* @return 返回 id
*/
public String getId()
{
return id;
}
/**
* @param 对id进行赋值
*/
public void setId(String id)
{
this.id = id;
}
/**
* @return 返回 className
*/
public String getClassName()
{
return className;
}
/**
* @param 对className进行赋值
*/
public void setClassName(String className)
{
this.className = className;
}
/**
* @return 返回 properties
*/
public List<PropsDefinition> getProperties()
{
return properties;
}
/**
* @param 对properties进行赋值
*/
public void setProperties(List<PropsDefinition> properties)
{
this.properties = properties;
}
}
|
/spring/ClientTest.java |
spring |
|
package spring;
import spring.simulator.LeamClassPathXMLApplicationContext;
public class ClientTest
{
/** <一句话功能简述>
* <功能详细描述>
* @param args
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args)
{
LeamClassPathXMLApplicationContext ctx = new LeamClassPathXMLApplicationContext("beans.xml");
PersonService personService = (PersonService)ctx.getBean("personService");
personService.save();
}
}
|
/beans.xml |
spring |
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="personDao" class="spring.PersonDaoBean"></bean>
<bean id="personService" class="spring.PersonServiceBean">
<property name="personDao" ref="personDao"></property>
</bean>
</beans>
|
/spring/PersonServiceBean.java |
spring |
|
package spring;
public class PersonServiceBean implements PersonService
{
private PersonDao personDao;
@Override
public void save()
{
personDao.add();
}
/**
* @return 返回 personDao
*/
public PersonDao getPersonDao()
{
return personDao;
}
/**
* @param 对personDao进行赋值
*/
public void setPersonDao(PersonDao personDao)
{
this.personDao = personDao;
}
}
|
/spring/PersonService.java |
spring |
|
package spring;
public interface PersonService
{
void save();
}
|
/spring/PersonDaoBean.java |
spring |
|
package spring;
public class PersonDaoBean implements PersonDao
{
@Override
public void add()
{
System.out.println("执行add()方法");
}
}
|
/spring/PersonDao.java |
spring |
|
package spring;
public interface PersonDao
{
void add();
}
|
/soa/InvocationProxy.java |
soa |
|
package soa;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.net.Socket;
public class InvocationProxy implements InvocationHandler
{
private String host;
private int port;
public InvocationProxy(String host, int port){
this.host = host;
this.port = port;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable
{
Socket socket = new Socket(host, port);
return null;
}
}
|
/soa/DataServiceImpl.java |
soa |
|
package soa;
public class DataServiceImpl implements DataService
{
@Override
public String getData(String key)
{
return "this is the data when key = " + key ;
}
}
|
/soa/DataService.java |
soa |
|
package soa;
public interface DataService
{
String getData(String key);
}
|
/ref/softcache/EmployeeCache.java |
ref |
|
package ref.softcache;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.HashMap;
public class EmployeeCache
{
static private EmployeeCache cache; //一个Cache实例
private HashMap<String, EmployeeRef> employeeRefs; //用于Cache内容的存储
private ReferenceQueue<Employee> q; //垃圾Reference的队列
/**
* 继承SoftReference,使得每一个实例都具有可识别的标识。
* 并且该标识与其在HashMap内的key相同。
*/
private class EmployeeRef extends SoftReference<Employee> {
private String _key = "";
public EmployeeRef(Employee em, ReferenceQueue<Employee> q) {
super(em, q);
_key = em.getId();
}
}
/**
* 构建一个缓存器实例
*/
private EmployeeCache() {
employeeRefs = new HashMap<String,EmployeeRef>();
q = new ReferenceQueue<Employee>();
}
/**
* 取得缓存器实例
*/
public static EmployeeCache getInstance() {
if (cache == null) {
cache = new EmployeeCache();
}
return cache;
}
/**
* 以软引用的方式对一个Employee对象的实例进行引用并保存该引用
*/
private void cacheEmployee(Employee em) {
cleanCache(); //清除垃圾引用
EmployeeRef ref = new EmployeeRef(em, q);
employeeRefs.put(em.getId(), ref);
}
/**
* 依据所指定的ID号,重新获取相应Employee对象的实例
*/
public Employee getEmployee(String id) {
Employee em = null;
//缓存中是否有该Employee实例的软引用,如果有,从软引用中取得。
if (employeeRefs.containsKey(id)) {
EmployeeRef employeeRef = employeeRefs.get(id);
em = employeeRef.get();
}
//如果没有软引用,或者从软引用中得到的实例是null,重新构建一个实例,
//并保存对这个新建实例的软引用
if (em == null) {
em = new Employee(id);
System.out.println("Retrieve From EmployeeInfoCenter. ID=" + id);
cacheEmployee(em);
}
return em;
}
/**
* 清除那些所软引用的Employee对象已经被回收的EmployeeRef对象
*/
private void cleanCache() {
EmployeeRef ref = null;
while((ref = (EmployeeRef) q.poll()) != null) {
employeeRefs.remove(ref._key);
}
}
/**
* 清除Cache内的全部内容
*/
public void clearCache() {
cleanCache();
employeeRefs.clear();
System.gc();
System.runFinalization();
}
}
|
/ref/softcache/Employee.java |
ref |
|
package ref.softcache;
public class Employee
{
private String id;// 雇员的标识号码
private String name;// 雇员姓名
private String department;// 该雇员所在部门
private String phone;// 该雇员联系电话
private int salary;// 该雇员薪资
private String origin;// 该雇员信息的来源
// 构造方法
public Employee(String id) {
this.id = id;
getDataFromlnfoCenter();
}
// 到数据库中取得雇员信息
private void getDataFromlnfoCenter() {
// 和数据库建立连接井查询该雇员的信息,将查询结果赋值
// 给name,department,phone,salary等变量
// 同时将origin赋值为"From DataBase"
}
/**
* @return 返回 id
*/
public String getId()
{
return id;
}
/**
* @param 对id进行赋值
*/
public void setId(String id)
{
this.id = id;
}
/**
* @return 返回 name
*/
public String getName()
{
return name;
}
/**
* @param 对name进行赋值
*/
public void setName(String name)
{
this.name = name;
}
/**
* @return 返回 department
*/
public String getDepartment()
{
return department;
}
/**
* @param 对department进行赋值
*/
public void setDepartment(String department)
{
this.department = department;
}
/**
* @return 返回 phone
*/
public String getPhone()
{
return phone;
}
/**
* @param 对phone进行赋值
*/
public void setPhone(String phone)
{
this.phone = phone;
}
/**
* @return 返回 salary
*/
public int getSalary()
{
return salary;
}
/**
* @param 对salary进行赋值
*/
public void setSalary(int salary)
{
this.salary = salary;
}
/**
* @return 返回 origin
*/
public String getOrigin()
{
return origin;
}
/**
* @param 对origin进行赋值
*/
public void setOrigin(String origin)
{
this.origin = origin;
}
}
|
/ref/AllTypeRefTest.java |
ref |
|
package ref;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
public class AllTypeRefTest
{
/**
* 即使显式调用了垃圾回收,但是用于 date 是强引用, date 没有被回收
*/
static void testStrongRef() {
MyDate date = new MyDate();
System.gc();
}
/**
* 在内存不足时,软引用被终止,等同于
* MyDate date = new MyDate();
* --------------- 由 JVM 决定运行 -----------------
* If(JVM. 内存不足 ()) {
* date = null;
* System.gc();
* }
* ----------------------------------------------
*/
static void testSoftRef() {
SoftReference ref = new SoftReference(new MyDate());
drainMemory(); // 让软引用工作
}
/**
* 在 JVM 垃圾回收运行时,弱引用被终止,等同于
* MyDate date = new MyDate();
* --------------- 垃圾回收运行 -----------------
* public void WeakSystem.gc() {
* date = null;
* System.gc();
* }
* ----------------------------------------------
*/
static void testWeakRef() {
WeakReference ref = new WeakReference(new MyDate());
System.gc(); // 让弱引用工作
}
/**
* 假象引用,在实例化后,就被终止了,等同于
* MyDate date = new MyDate();
* date = null;
* --------------- 终止点,在实例化后,不是在 gc 时,也不是在内存不足时 -----------------
*/
static void testPhantomRef() {
ReferenceQueue queue = new ReferenceQueue();
PhantomReference ref = new PhantomReference(new MyDate(), queue);
System.gc(); // 让假象引用工作
}
private static void drainMemory()
{
// TODO Auto-generated method stub
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
}
}
class MyDate
{
}
|
/ref/ParamPassTest2.java |
ref |
|
package ref;
public class ParamPassTest2
{
//基本类型的参数传递
public static void testBasicType(int m) {
System.out.println("m=" + m); //m=50
m = 100;
System.out.println("m=" + m); //m=100
}
public static void add(StringBuffer s) {
s.append("_add");
}
public static void changeRef(StringBuffer s) {
s = new StringBuffer("Java");
}
public static void main(String[] args)
{
int i = 50;
testBasicType(i);
System.out.println(i); //i=50
StringBuffer sMain = new StringBuffer("init");
System.out.println("sMain=" + sMain.toString()); //sMain=init
add(sMain);
System.out.println("sMain=" + sMain.toString()); //sMain=init_add
changeRef(sMain);
System.out.println("sMain=" + sMain.toString());//sMain=init_add
}
}
|
/ref/ParamPassTest.java |
ref |
|
package ref;
import java.util.ArrayList;
import java.util.List;
public class ParamPassTest
{
static void addList(List s) {
s.add("dfdsa");
}
static void testList() {
List<String> list = new ArrayList<String>();
list.add("abc");
addList(list);
System.out.println(list); // 此处结果为[abc, dfdsa]
}
static void addString(String s) {
s += "dfdsa";
}
static void testString() {
String a = "abc";
addString(a);
System.out.println(a); // 此处结果为abc
}
static void compareString() {
String s1 = new String("abc");
String s2 = s1.intern(); //在heap里面创建一个完全一样的String对象,并且将该对象的引用放入String Pool中,最后返回给调用方
String s3 = "abc";
System.out.println(s1==s2); //false
System.out.println(s2==s3); //true
System.out.println(s1==s3); //false
}
static void testStringBuffer() {
StringBuffer s = new StringBuffer("Java");
StringBuffer s1 = s;
s1.append(" World");
System.out.println("s1=" + s1.toString());//打印结果为:s1=Java World
System.out.println("s=" + s.toString());//打印结果为:s=Java World
}
public static void main(String[] args)
{
testList();
testString();
compareString();
testStringBuffer();
}
}
|
/pattern/template/old/HappyPeople.java |
pattern, template |
|
package pattern.template.old;
public class HappyPeople
{
public void celebrateSpringFestival() {
System.out.println("Buying ticket");
System.out.println("Travlling by train");
System.out.println("Happy Chinese New Year");
}
}
|
/pattern/template/callback/CallbackTest.java |
pattern, template |
|
package pattern.template.callback;
import pattern.template.HappyPeople;
public class CallbackTest
{
public static void main(String[] args)
{
HappyPeople byAir = new HappyPeople(){
@Override
protected void travel() {
System.out.println("Travelling by air");
}
};
HappyPeople byTrain = new HappyPeople(){
@Override
protected void travel() {
System.out.println("Travelling by train");
}
};
System.out.println("Let's go home!");
byAir.celebrateSpringFestival();
byTrain.celebrateSpringFestival();
}
}
|