Java 在主线程上运行的fusedLocationProvider,即使该方法是从不同的可运行线程调用的 autoSearch_按钮。setOnClickListener(视图->{ 授予许可(); 选中位置启用或不启用(); lastButton_pressed=view.getId(); ExampleRunnable ExampleRunnable=新的ExampleRunnable(20); 线程线程=新线程(exampleRunnable); thread.start(); thread.join(); }); 公共类ExampleRunnable实现Runnable{ 私有int-kmRadius; 专用双lat=0,longi=0; 公共示例可运行(int i){ 这个.kmRadius=i; } @凌驾 公开募捐{ lastLocation(); } 私有void lastLocation(){ Log.i(标记“lastLocation:+Thread.currentThread().getName()); fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(MainActivity.this); //现在假设我已经宣布 @SuppressLint(“MissingPermission”)任务位置Task=fusedLocationProviderClient.getLastLocation(); locationTask.addOnSuccessListener(位置->{ 如果(位置!=null){ //我们有一个位置 Log.d(标记“run:last location”+Thread.currentThread().getName());); this.lat=location.getLatitude(); this.longi=location.getLongitude(); 打印(); }否则{ d(标记“onSuccess:Location为null…调用robust”); } }).addOnFailureListener(e->Log.e(标记“onFailure:+e.getLocalizedMessage()); } } 公共同步作废打印(){ Log.d(标记“print:+Thread.currentThread.getName()); } }

Java 在主线程上运行的fusedLocationProvider,即使该方法是从不同的可运行线程调用的 autoSearch_按钮。setOnClickListener(视图->{ 授予许可(); 选中位置启用或不启用(); lastButton_pressed=view.getId(); ExampleRunnable ExampleRunnable=新的ExampleRunnable(20); 线程线程=新线程(exampleRunnable); thread.start(); thread.join(); }); 公共类ExampleRunnable实现Runnable{ 私有int-kmRadius; 专用双lat=0,longi=0; 公共示例可运行(int i){ 这个.kmRadius=i; } @凌驾 公开募捐{ lastLocation(); } 私有void lastLocation(){ Log.i(标记“lastLocation:+Thread.currentThread().getName()); fusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(MainActivity.this); //现在假设我已经宣布 @SuppressLint(“MissingPermission”)任务位置Task=fusedLocationProviderClient.getLastLocation(); locationTask.addOnSuccessListener(位置->{ 如果(位置!=null){ //我们有一个位置 Log.d(标记“run:last location”+Thread.currentThread().getName());); this.lat=location.getLatitude(); this.longi=location.getLongitude(); 打印(); }否则{ d(标记“onSuccess:Location为null…调用robust”); } }).addOnFailureListener(e->Log.e(标记“onFailure:+e.getLocalizedMessage()); } } 公共同步作废打印(){ Log.d(标记“print:+Thread.currentThread.getName()); } },java,android,multithreading,performance,location,Java,Android,Multithreading,Performance,Location,在logcat中输出我想要的内容 最后位置:thread4 运行:lastLocation thread4 打印:thread4 但是我得到了什么结果呢- 最后位置:thread4// 运行:lastLocation主// 打印:主 我想处理位置并在特定线程中处理它 locationTask.addOnSuccessListener 这不会运行侦听器,它只是注册locationTask对象后面的代码块,该对象可以用它做任何事情 显然(这在像这样的事件处理程序系统中很常见),一些线程最终会执行

在logcat中输出我想要的内容

  • 最后位置:thread4
  • 运行:lastLocation thread4
  • 打印:thread4
但是我得到了什么结果呢- 最后位置:thread4// 运行:lastLocation主// 打印:主

我想处理位置并在特定线程中处理它

locationTask.addOnSuccessListener

这不会运行侦听器,它只是注册
locationTask
对象后面的代码块,该对象可以用它做任何事情

显然(这在像这样的事件处理程序系统中很常见),一些线程最终会执行某个事件,因此,该事件的侦听器会在该线程中当场运行

您有两种解决方案:

  • 导致任何事件最终触发侦听器(您的代码无法帮助解释其位置;导致变量
    locationTask
    指向的对象进入“成功”状态,从而触发侦听器的事件)发生在您希望它发生的线程中,而不是主线程中

  • 不要启动线程来注册成功的侦听器;在成功侦听器中启动一个线程(因此,不要启动
    Log.d(标记“run:last location”…
    ),而是启动一个线程)

  • 有时,事件处理程序系统是可配置的,您可以告诉它在其他线程中触发此类事件,但这种情况很少见。如果库不支持它(我怀疑它支持),则必须编写包装器方法或包装整个库才能获得此功能

      autoSearch_button.setOnClickListener(view -> {
            grant_permission();
            check_location_enableornot();
            lastButton_pressed = view.getId();
            ExampleRunnable exampleRunnable = new ExampleRunnable(20);
            Thread thread = new  Thread(exampleRunnable);
            thread.start();
            thread.join();
        });
     
      public class ExampleRunnable implements Runnable {
        private int kmRadius;
        private double lat =0 , longi =0 ;
    
        public ExampleRunnable(int i) {
            this.kmRadius = i;
        }
    
        @Override
        public void run() {
            lastLocation();
        }  
        
        private void lastLocation(){
            Log.i(TAG, "lastLocation: " + Thread.currentThread().getName());
            
           fusedLocationProviderClient =  LocationServices.getFusedLocationProviderClient(MainActivity.this);
           // for now being assume that i have declared     
          @SuppressLint("MissingPermission") Task<Location> locationTask = fusedLocationProviderClient.getLastLocation();
            locationTask.addOnSuccessListener(location -> {
                if (location != null) {
                    //We have a location
                    Log.d(TAG, "run: last location" +  Thread.currentThread().getName()););
                    this.lat = location.getLatitude();
                    this.longi = location.getLongitude();
                    print();
                } else  {
                    Log.d(TAG, "onSuccess: Location was null... calling robust");
                }
            }).addOnFailureListener(e -> Log.e(TAG, "onFailure: " + e.getLocalizedMessage() ));
          }
        }
       
        public synchronized void print(){
          Log.d(TAG, "print: " + Thread.currentThread.getName());
        }
     }