三道题,分别涉及反射、多线程处理、数字精度
反射
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ShowMeBug {
public interface IA{
String getName();
}
public static void main(String[] args) {
IA ia = (IA) checkName(IA.class.getName() + "&getName=Abc");
System.out.println(ia.getName());
IA ia2 = (IA) checkName(IA.class.getName() + "&getTest=ers");
System.out.println(ia2.getName());
}
private static Object checkName(String str){
String[] split = str.split("&", -1);
String[] strings = split[1].split("=", -1);
String check = strings[0];
String result = strings[1];
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals(check)){
return result;
}
return null;
}
};
return Proxy.newProxyInstance(IA.class.getClassLoader(), new Class[]{ IA.class }, invocationHandler);
}
}
多线程处理
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Method;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ShowMeBug {
private static final Logger LOGGER = LoggerFactory.getLogger(ShowMeBug.class);
private double balance;
public void deposit(double money){
synchronized (this){
double nowBalance = getBalance();
balance = nowBalance + money;
LOGGER.warn("ID: " + String.valueOf(Thread.currentThread().getId())+" >>>>>> NAME: " + Thread.currentThread().getName());
}
}
private double getBalance() {
return balance;
}
public static void main(String[] args) {
try {
final Class<ShowMeBug> showMeBugClass = ShowMeBug.class;
Object newInstance = showMeBugClass.newInstance();
BlockingQueue<Runnable> blockingQueue = new LinkedBlockingDeque<>(10);
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(20, 100, 60, TimeUnit.SECONDS, blockingQueue);
threadPoolExecutor.prestartAllCoreThreads();
for (int i = 0; i < 100; i++) {
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
try {
Method deposit = showMeBugClass.getMethod("deposit", double.class);
deposit.invoke(newInstance, 1);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
threadPoolExecutor.shutdown();
threadPoolExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MINUTES);
System.out.println(showMeBugClass.getDeclaredField("balance").get(newInstance));
} catch (Exception e) {
e.printStackTrace();
}
}
}
数字精度
import java.math.BigDecimal;
import java.text.DecimalFormat;
public class ShowMeBug {
public static void main(String[] args) {
double a = 3;
double b = 2222222222222222222222.0;
double i = a / b;
System.out.println(i);
DecimalFormat decimalFormat = new DecimalFormat("0.00000000000000000000000000000000000000000000000000000000000000");
System.out.println(decimalFormat.format(i));
BigDecimal bigDecimal = new BigDecimal(i);
String plainString = bigDecimal.toPlainString();
System.out.println(plainString);
String format = String.format("%.45f", Double.valueOf(plainString));
System.out.println(format);
System.out.println(plainString.substring(0, 45));
}
}