这一篇我还是继续上一篇没有讲完的内容,先上一个例子:

private static final int threadCount = 100;  public static void main(String[] args) {     initFlowRule();      for (int i = 0; i < threadCount; i++) {         Thread entryThread = new Thread(new Runnable() {             @Override             public void run() {                 while (true) {                     Entry methodA = null;                     try {                         TimeUnit.MILLISECONDS.sleep(5);                         methodA = SphU.entry("methodA");                        } catch (BlockException e1) {                         // Block exception                     } catch (Exception e2) {                         // biz exception                     } finally {                          if (methodA != null) {                             methodA.exit();                          }                     }                 }             }         });         entryThread.setName("working thread");         entryThread.start();     } }   private static void initFlowRule() {     List<FlowRule> rules = new ArrayList<FlowRule>();     FlowRule rule1 = new FlowRule();     rule1.setResource("methodA");     // set limit concurrent thread for 'methodA' to 20     rule1.setCount(20);     rule1.setGrade(RuleConstant.FLOW_GRADE_THREAD);     rule1.setLimitApp("default");      rules.add(rule1);     FlowRuleManager.loadRules(rules); } 

SphU#entry

我先把例子放上来

Entry methodA = null; try {      methodA = SphU.entry("methodA");       // dosomething  } catch (BlockException e1) {     block.incrementAndGet(); } catch (Exception e2) {     // biz exception } finally {     total.incrementAndGet();     if (methodA != null) {         methodA.exit();      } }

我们先进入到entry方法里面:
SphU#entry

public static Entry entry(String name) throws BlockException {     return Env.sph.entry(name, EntryType.OUT, 1, OBJECTS0); }

这个方法里面会调用Env的sph静态方法,我们进入到Env里面看看

public class Env {      public static final Sph sph = new CtSph();      static {         // If init fails, the process will exit.         InitExecutor.doInit();     } }

这个方法初始化的时候会调用InitExecutor.doInit()
InitExecutor#doInit