1.目前有一套系统的一个功能查询速度慢,基于缓存设计一套解决方案。
2.手写一段死锁代码
public class ThreadA implements Runnable {
private Object object1;
private Object object2;
public ThreadA(Object object1, Object object2) {
this.object1 = object1;
this.object2 = object2;
}
@Override
public void run() {
synchronized (object1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (object2) {
System.out.println(Thread.currentThread().getName() + " has execute");
}
}
}
}
public class ThreadB implements Runnable {
private Object object1;
private Object object2;
public ThreadB(Object object1, Object object2) {
this.object1 = object1;
this.object2 = object2;
}
@Override
public void run() {
synchronized (object2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (object1) {
System.out.println(Thread.currentThread().getName() + " has executed");
}
}
}
}
public class ThreadTest {
public static void main(String[] args) {
Object obj1 = new Object();
Object obj2 = new Object();
Thread t1 = new Thread(new ThreadA(obj1, obj2));
Thread t2 = new Thread(new ThreadB(obj1, obj2));
t1.start();
t2.start();
}
}
3.手写一段ioc容器
容器实现类 SimpleIOC 的代码:
public class SimpleIOC {
private Map<String, Object> beanMap = new HashMap<>();
public SimpleIOC(String location) throws Exception {
loadBeans(location);
}
public Object getBean(String name) {
Object bean = beanMap.get(name);
if (bean == null) {
throw new IllegalArgumentException("there is no bean with name " + name);
}
return bean;
}
private void loadBeans(String location) throws Exception {
// 加载 xml 配置文件
InputStream inputStream = new FileInputStream(location);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(inputStream);
Element root = doc.getDocumentElement();
NodeList nodes = root.getChildNodes();
// 遍历 <bean> 标签
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
String id = ele.getAttribute("id");
String className = ele.getAttribute("class");
// 加载 beanClass
Class beanClass = null;
try {
beanClass = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
}
// 创建 bean
Object bean = beanClass.newInstance();
// 遍历 <property> 标签
NodeList propertyNodes = ele.getElementsByTagName("property");
for (int j = 0; j < propertyNodes.getLength(); j++) {
Node propertyNode = propertyNodes.item(j);
if (propertyNode instanceof Element) {
Element propertyElement = (Element) propertyNode;
String name = propertyElement.getAttribute("name");
String value = propertyElement.getAttribute("value");
// 利用反射将 bean 相关字段访问权限设为可访问
Field declaredField = bean.getClass().getDeclaredField(name);
declaredField.setAccessible(true);
if (value != null && value.length() > 0) {
// 将属性值填充到相关字段中
declaredField.set(bean, value);
} else {
String ref = propertyElement.getAttribute("ref");
if (ref == null || ref.length() == 0) {
throw new IllegalArgumentException("ref config error");
}
// 将引用填充到相关字段中
declaredField.set(bean, getBean(ref));
}
// 将 bean 注册到 bean 容器中
registerBean(id, bean);
}
}
}
}
}
private void registerBean(String id, Object bean) {
beanMap.put(id, bean);
}
}
容器测试使用的 bean 代码:
public class Car {
private String name;
private String length;
private String width;
private String height;
private Wheel wheel;
// 省略其他不重要代码
}
public class Wheel {
private String brand;
private String specification ;
// 省略其他不重要代码
}
bean 配置文件 ioc.xml 内容:
<beans>
<bean id="wheel" class="com.titizz.simulation.toyspring.Wheel">
<property name="brand" value="Michelin" />
<property name="specification" value="265/60 R18" />
</bean>
<bean id="car" class="com.titizz.simulation.toyspring.Car">
<property name="name" value="Mercedes Benz G 500"/>
<property name="length" value="4717mm"/>
<property name="width" value="1855mm"/>
<property name="height" value="1949mm"/>
<property name="wheel" ref="wheel"/>
</bean>
</beans>
IOC 测试类 SimpleIOCTest:
public class SimpleIOCTest {
@Test
public void getBean() throws Exception {
String location = SimpleIOC.class.getClassLoader().getResource("spring-test.xml").getFile();
SimpleIOC bf = new SimpleIOC(location);
Wheel wheel = (Wheel) bf.getBean("wheel");
System.out.println(wheel);
Car car = (Car) bf.getBean("car");
System.out.println(car);
}
}
4.有3个方法A、B、C。基于aop打印每个方法的执行时间
@Aspect
@Component
public class TimeInterceptor {
private static Logger LOGGER = LoggerFactory.getLogger(TimeInterceptor.class);
public static final String POINT = "execution(* *..*Service.*(..))";
@Around(POINT)
public Object timeAround(ProceedingJoinPoint joinPoint) {
Object obj = null;
Object[] args = joinPoint.getArgs();
long startTime = System.currentTimeMillis();
try {
obj = joinPoint.proceed(args);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
long endTime = System.currentTimeMillis();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
printExecTime(methodName, startTime, endTime);
return obj;
}
private void printExecTime(String methodName, long startTime, long endTime) {
long diffTime = endTime - startTime;
LOGGER.info(methodName + ":" + diffTime + " :ms");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.ken"/>
</beans>
@Service
public class BookService {
public void save() {
System.out.println("BookService save()");
}
}
public class Demo {
@Test
public void run2() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) ac.getBean("bookService");
bookService.save();
}
}
5.怎样实现一套分布式服务
6.HashMap碰撞解决方案
7.Restful的底层实现原理是什么
8.怎样描述面向对象