简体   繁体   English

计算方法被调用了多少次

[英]count how many times is method called

How can I count how many times is dependency.startService();我如何计算依赖项的次数。startService(); method call?方法调用? Different Services are calling this method and I don't want to get how many time did everyone call that method but single Service.不同的服务正在调用此方法,我不想知道每个人都调用了该方法多少次,但只调用了单个服务。 I should get this output :我应该得到这个输出:

My name is Service B and I'm depending on Service A
My name is Service C and I'm depending on Service A
My name is Service D and I'm depending on Service B

***Service Service C lets start!***
   1

***Service Service D lets start!***
   2

Actually this number should mean how many services these two are depending on.实际上,这个数字应该表示这两个依赖的服务数量。 Do you have any ideas on how can I do this?你对我该怎么做有什么想法吗? I have tried and I can only get a global number of calling that method which is 3.我试过了,我只能获得调用该方法的全局数量,即 3。

Here is my code:这是我的代码:

 ManagerService.java
    import java.util.*;
    import java.util.concurrent.CountDownLatch;

    public class ManagerService
    {
        public static void main(String[] args) throws InterruptedException
        {
            //Creating Services
            Service serviceA = new Service("Service A", "Thread A");
            Service serviceB = new Service("Service B", "Thread B");
            Service serviceC = new Service("Service C", "Thread C");
            Service serviceD = new Service("Service D", "Thread D");

            serviceB.dependesOn(serviceA);
            serviceC.dependesOn(serviceA);
            serviceD.dependesOn(serviceB);    

            System.out.println();
            System.out.println("***Service " + serviceC.serviceName +" lets start!***");
            serviceC.startService();
            System.out.println();
            System.out.println("***Service " + serviceD.serviceName +" lets start!***");
            serviceD.startService();

        }
    }

and 
Service.java

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class Service
{
    public String serviceName;
    public String threadName;
    private boolean onOrOff = false;
    public List <Service> dependentServicesOn = new ArrayList <Service>();
    public CountDownLatch startSignal;
    private Integer counter = 0;
    public Service(String service_name, String thread_name)
    {
        this.serviceName = service_name;
        this.threadName = thread_name;
    }

    public void dependesOn(Service s) throws InterruptedException
    {
        System.out.println("My name is " + serviceName +" and i'm depending on " + s.serviceName);
        dependentServicesOn.add(s);   
    }

    public Service startService() throws InterruptedException
    {
        for(Service dependency : dependentServicesOn) {
            if(!dependency.isStarted()) {
                dependency.startService();
            }
        }

        startSignal = new CountDownLatch(1);
       // new Thread(new CreateThread(this,startSignal)).start();
        startSignal.countDown();
        return null;
    }

    public boolean isStarted()
    {
        return onOrOff;
    }

    public void setStarted()
    {
        onOrOff = true;
    }
}

You can set a variable count .您可以设置一个变量count For each method call increase the value by one.对于每个方法调用,将值加一。 If you want to access the variable from outside of the class you can set it public static.如果要从类外部访问变量,可以将其设置为 public static。

So something like this can be done所以可以做这样的事情

public static long count = 0;

public Service startService() throws InterruptedException
    {
       count++;
       // method tasks
    }

When you need to check you can check the count variable.当您需要检查时,您可以检查count变量。

Each Service already has a List that stores the "parent" (services on which it is dependent).每个服务已经有一个存储“父”(它所依赖的服务)的列表。 So, the size of that list is the count of direct parents.因此,该列表的大小是直接父母的数量。 Since you also want to go further and find indirect dependencies, you can do it by "asking" each parent service how many services it depends upon.由于您还想更进一步寻找间接依赖关系,您可以通过“询问”每个父服务它所依赖的服务数量来实现。

The code would look something like this:代码看起来像这样:

public int getCountOfDependencies()
{
   int theCount = 0;

   for (Service nxtService : dependentServicesOn)
   {
      theCount++; //Add one for the "direct parent"

      theCount += nxtService.getCountOfDependencies();  //Also add grand-parents etc.
   }
   return theCount;
}

Warning!警告! This will not work if a service can be dependent on another service via two or more "paths".如果一个服务可以通过两个或多个“路径”依赖于另一个服务,这将不起作用。 For instance, consider this scenario:例如,考虑这个场景:

serviceB.dependesOn(serviceA);
serviceC.dependesOn(serviceA);
serviceD.dependesOn(serviceB);
serviceD.dependesOn(serviceC);

Now, A is a "grand-parent" of D in two ways.现在, A在两个方面是D的“祖父母”。 So, if you call getCountOfDependencies() on D , it will count the direct parents ( B and C ) and will ask each of those to report their dependencies.因此,如果您在D上调用 getCountOfDependencies() ,它将计算直接父级( BC )并要求每个父报告它们的依赖关系。 They will each report 1, and thus A will be double-counted.他们将各自报告 1,因此A将被重复计算。

So, if you can have that type of situation, you will have to modify the approach.因此,如果您可以遇到这种情况,则必须修改方法。

If I understand the question correctly, the following should work.如果我正确理解了这个问题,以下应该有效。

Add an ArrayList<String> dependentServices as a class member.添加一个ArrayList<String> dependentServices作为类成员。

Change startService() to startService(String callerName) .startService()更改为startService(String callerName)

When starting a service, for example serviceA, call serviceA.startService(serviceName);启动服务时,例如serviceA,调用serviceA.startService(serviceName);

In your startService(...) method add the following:在您的 startService(...) 方法中添加以下内容:

boolean dependent = false;

for(int i = 0; i < dependentServices.size(); i++) {

    if(dependentServices.get(i).equals(callerName) {
        dependent = true;
    }
}

if(!dependent) {
    dependentServices.add(callerName);
}

In each service, the ArrayList dependentServices will now keep a list of the names of the services which have called it.在每个服务中,ArrayListdependentServices 现在将保留调用它的服务名称的列表。 The size of the ArrayList will tell you how many services there are. ArrayList 的大小会告诉您有多少服务。

Simply have a StaticCounter class and use that for identifying counts by incrementing the values.只需拥有一个 StaticCounter 类并使用它来通过增加值来识别计数。

StaticCounter.java静态计数器.java

public class StaticCounter {
  static int COUNTER;
}

JTry.java JTry.java

public class JTry {
  public static void main(String[] args) {
    for(int i = 0; i<5; i++) {
      System.err.println("Passing checkpoint " + StaticCounter.COUNTER++);
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM