Dependency Injection if service exists Is there any way to use dependency injection in a conditional way, i.e. only if a service exists similar to \Drupal::hasService().

I have a created a service in module A and in module B I have an EventSubscriber that should use the service from module A, but only if present of course.

Static I would do sth. like:
    if (\Drupal::hasService('module_a.my_service')) {
      \Drupal::service('module_a.my_service')->mymethod();
    }

Is there any possibility to do this when injecting the service?
One possible way to handle this situation is for your Module B to define a default, stub service with the same type signature (ie, Interface) as the one you need).  Use that in the YAML.  Then, in your Module A, you can alter the service to the one that the module provides.

That said, I do agree with the comments that you may be bypassing one of the benefits of DI, and creating a tight coupling between your two modules.  Read the linked article to see when it is appropriate.  Core uses it for the language and workspaces subsystems.