关于网络请求调用函数的一个小实验

通常网络App应用向服务器请求都会带一个Method的参数,表示要调用的方法


今日Monday一时兴起,想试验下各种Method调用的方式,一共分3种:

1、用switch查找要调用的方法

2、用if else 查找要调用的方法

3、用反射方法查找方法。


if else 和 switch 不相伯仲

反射方法相差太远了。



代码很简单,,起了3个线程,分别循环调用一个空函数,最后输出时间。代码如下:
package com.monday.switchandfunctiontest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Main {

	private static boolean mIsSwitchEnded = false;
	private static boolean mIsFunctionEnded = false;
	private static boolean mIsIfElseEnded = false;
	private static final int COUNT = Integer.MAX_VALUE;
	
	public static void main(String[] args) {
		
		new FunctionThread().start();
		new SwitchThread().start();
		new IfelseThread().start();
		while(!mIsSwitchEnded || !mIsFunctionEnded || !mIsIfElseEnded); //等待3个线程结束再退出程序
	}
	
	static class FunctionThread extends Thread{
		@Override
		public void run() {
			super.run();
			long start = System.currentTimeMillis();
			Class c = null;
			try {
				c = Class.forName("com.monday.switchandfunctiontest.Main");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				mIsSwitchEnded = true;
				return;
			}
			for(int i = 0 ; i < COUNT ; i ++) { //for 循环模拟高并发
				// 将方法写在循环内,是因为方法名是在请求接受到后才获得,这里是模拟获取到函数名字
				final String funStr = "functionFun";  
				
				Method m;
				try {
					m = c.getMethod(funStr, null);
					m.invoke(null, null);
				} catch (NoSuchMethodException | SecurityException e) {
					e.printStackTrace();
					mIsSwitchEnded = true;
					return;
				} catch (IllegalAccessException e) {
					e.printStackTrace();
					mIsSwitchEnded = true;
					return;
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
					mIsSwitchEnded = true;
					return;
				} catch (InvocationTargetException e) {
					e.printStackTrace();
					mIsSwitchEnded = true;
					return;
				}
			}
			
			
			System.out.println("FunctionThread end at " + (System.currentTimeMillis() - start));
			mIsSwitchEnded = true;
		}
	}
	
	static class SwitchThread extends Thread{
		@Override
		public void run() {
			super.run();
			long start = System.currentTimeMillis();
			
			for(int i = 0; i < COUNT; i++){
				final int action = 10; // 假设获得了action
				switchFun(action); //do the action
			}
			System.out.println("SwitchThread end at " + (System.currentTimeMillis() - start));
			mIsFunctionEnded = true;
		}
	}
	
	static class IfelseThread extends Thread{
		@Override
		public void run() {
			super.run();
			long start = System.currentTimeMillis();
			
			for(int i = 0; i < COUNT; i++){
				final int action = 10; // 假设获得了action
				ifelseFun(action); //do the action
			}
			System.out.println("IfelseThread end at " + (System.currentTimeMillis() - start));
			mIsIfElseEnded = true;
		}
	}
	
	static void switchFun(int action){
		switch(action){
		case 0:break;
		case 1:break;
		case 2:break;
		case 3:break;
		case 4:break;
		case 5:break;
		case 6:break;
		case 7:break;
		case 8:break;
		case 9:break;
		case 10:
			functionFun();
			break;
		case 11:break;
		case 12:break;

		}
	}
	
	static void ifelseFun(int action){
		if(action ==0){}
		else if(action ==1){}
		else if(action ==2){}
		else if(action ==3){}
		else if(action ==4){}
		else if(action ==5){}
		else if(action ==6){}
		else if(action ==7){}
		else if(action ==8){}
		else if(action ==9){}
		else if(action ==10){functionFun();}
		else if(action ==11){}
		else if(action ==12){}
	}
	
	public static void functionFun(){  //需要被调用的方法。
		// do something
	}
}


输出结果:

IfelseThread end at 17282
SwitchThread end at 18812


反射的方法等了很久都没有输出。

猜你喜欢

转载自blog.csdn.net/dwdyoung/article/details/24303323
今日推荐