How many times do you call the method

How can I calculate how the dependencies can be time.startService (); method call? Different services call this method, and I do not want every time everyone calls this method, but only one 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 

In fact, this number should mean how many services depend on this. Do you have any idea how I can do this? I tried, and I can only get the global call number of this method, which is 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; } } 
+4
source share
4 answers

You can set the variable count . For each method call, increase the value by one. If you want to access a variable from outside the class, you can set it to public static.

So, you can do something like this

 public static long count = 0; public Service startService() throws InterruptedException { cout++; // method tasks } 

When you need to check, you can check the count variable

+3
source

Each service already has a List in which the "parent" is stored (the services it depends on). Thus, the size of this list is the number of direct parents. Since you also want to go further and find indirect dependencies, you can do this by asking each parent service how many services it depends on.

The code looks 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; } 

A warning! This will not work if the service can depend on another service through two or more "paths". For example, consider this scenario:

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

Now A is the "parent" of D in two ways. Thus, if you call getCountOfDependencies () on D , it will count the direct parents ( B and C ) and will ask each of them to report its dependencies. Each of them will report 1, and thus A will be counted with a double count.

So, if you may have this situation, you will have to change the approach.

+1
source

If I understand the question correctly, the following should work.

Add ArrayList<String> dependentServices as a member of the class.

Change startService() to startService(String callerName) .

When starting a service, such as serviceA, call serviceA.startService(serviceName);

In your startService (...) method add the following:

 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 dependent ArrayList services will now contain a list of the names of the services that called it. The size of an ArrayList will show you how many services exist.

0
source

Just use the StaticCounter class and use it to define counters by incrementing values.

StaticCounter.java

 public class StaticCounter { static int COUNTER; } 

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++); } } 
0
source

Source: https://habr.com/ru/post/1488719/


All Articles