android中的线程同步

android中的线程同步,android,multithreading,thread-synchronization,Android,Multithreading,Thread Synchronization,我有两种服务 服务A 服务B 服务c 服务代码: public class ServiceA extends Service { @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); testfunction1(); Thread1(); } public

我有两种服务

  • 服务A
  • 服务B
  • 服务c
  • 服务代码:

    public class ServiceA extends Service  {
    
    
        @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        testfunction1();
                Thread1();
    
    }
    
    public synchronized void testfunction1(){
        Log.d("Thread","Hi I Am Test 1");
    }
    
    public void Thread1(){
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5000);
    
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    testfunction1();
    
                }
              }                        
            }).start();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    
    服务B代码

    public class ServiceB extends Service  {
    
    
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    
    @Override
    public void onCreate() {
        super.onCreate();
        testfunction2();
                Thread2();
    }
    
    public synchronized void testfunction2(){
        Log.d("Thread","Hi I Am Test 2");
    }
    
    public void Thread2(){
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5000);
    
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    testfunction2();
    
                }
              }                        
            }).start();
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    
    编辑:新增服务3

    **Service C Code**
    
        public class ServiceC extends Service  {
    
    
        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            Intent ServiceA = new Intent("ServiceA Package Name");
            this.startService(ServiceA);
            Intent ServiceB = new Intent("ServiceB Package Name");
            this.startService(ServiceB);
            ServiceA admin = new ServiceA();
            admin.Thread1();
            ServiceB security = new ServiceB();
            security.Thread2();
        }
    }
    
    问题是当我运行这两个服务时

    它将继续记录

    线程嗨,我是测试1

    线程嗨,我是测试1

    线程嗨,我是测试1

    线程嗨,我是测试1

    线程嗨,我是测试1

    线程嗨,我是测试1


    为什么我没有获取Service 2日志?

    您是否在代码中的任何地方调用Thread1和Thread2?您如何创建这些服务?startService?bindService?我认为当服务启动时,它将在后台继续运行代码,并且您已经同步了testFunction1()。因此,它将不允许其他函数运行。@smichak我在ServiceA OnCreate()中调用了Thread1,在ServiceB OnCreate()中调用了Thread2。您的OnCreate方法只调用testFunction1和testFunction2,这两个函数打印行。。。我根本不知道线程是如何创建的。我遗漏了什么吗?如果您在
    OnCreate()
    方法中调用了类
    ServiceA
    Thread1
    方法,请更新您的代码。