Android 如何使用处理程序线程进行多线程

Android 如何使用处理程序线程进行多线程,android,multithreading,android-handler,Android,Multithreading,Android Handler,我试图在谷歌地图上移动多个标记,每个标记都在单独的线程中。目前,我正在使用java线程类inside loop为每辆车创建单独的线程。以下是示例代码: private void proceedToMoveVehicle(ArrayList<RealTimeDetailsResponce> sourceList, ArrayList<RealTimeDetailsResponce> destinationList) { for (int i = 0; i <

我试图在谷歌地图上移动多个标记,每个标记都在单独的线程中。目前,我正在使用java线程类inside loop为每辆车创建单独的线程。以下是示例代码:

private void proceedToMoveVehicle(ArrayList<RealTimeDetailsResponce> sourceList, ArrayList<RealTimeDetailsResponce> destinationList) {
    for (int i = 0; i < destinationList.size(); i++) {
        for (int j = 0; j < sourceList.size(); j++) {
            if (destinationList.get(i).getVehicleNo().equalsIgnoreCase(sourceList.get(j).getVehicleNo())) {
                final com.google.maps.model.LatLng vehicleStartPositions = new com.google.maps.model.LatLng(sourceList.get(j).getLatitude(), sourceList.get(j).getLongitude());
                final double destainationLatitude = destinationList.get(i).getLatitude();
                final double destainationLongitude = destinationList.get(i).getLongitude();
                final String vehicleTitle = sourceList.get(j).getVehicleNo();
                final com.google.maps.model.LatLng vehicleEndPositions = new com.google.maps.model.LatLng(destainationLatitude, destainationLongitude);
                final double speedValue = 0.0000;
                if (vehicleStartPositions.lat != vehicleEndPositions.lat || vehicleStartPositions.lng != vehicleEndPositions.lng) {
                    Thread thread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            getPathyFromDirectionAPiRequestNew(vehicleStartPositions, vehicleEndPositions, vehicleTitle);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    moveVehicle(vehicleTitle, speedValue);
                                }
                            });
                        }
                    });
                    thread.start();
                }
            }
        }
    }
}
private void proceedtomovevevehicle(ArrayList sourceList,ArrayList destinationList){
对于(int i=0;i
我每5秒从服务器上获取车辆的更新位置。这里的缺点是,一旦我得到更新的位置,我就不能再次重用这些线程,我必须再次创建一个新线程

我听说过安卓处理器线程,因为我们可以使用Looper重用线程。有人能帮我处理处理线程吗。

试试这个:

ExecutorService executorService = Executors.newFixedThreadPool(32); // you can number of threads you want

executorService.execute(new Runnable() {
public void run() {
         //do your work1 here
    } 
});

executorService.execute(new Runnable() {
public void run() {
         //do your work2 here and so on..
    } 
});

或者您可以设置一个循环。

由于
IntentService
正在
HandlerThread
上运行,因此我假设这将回答问题


但是
HandlerThread
的问题是它在活动的生命周期之外运行(也就是
IntentService
),因此需要正确清理它,否则会出现线程泄漏。

你不是在说把它放在类中吗?不,我在问有关处理程序线程的问题。为什么不在不同的线程中使用executorservice?好的,请您分享executorservice的示例代码。我的问题是,为什么要这样做?在我看来,这可能是最糟糕的方法,首先你无法控制线程,如果你关闭活动/片段,并且有正在运行的线程,你可能会崩溃,在这里生成大量线程不会加快应用程序的运行速度,可能会得到更坏的结果,因为你从所有线程更新UI线程,我建议将更新捆绑在异步任务或作业调度器中,并在更新一组CAR时更新UI线程。您能告诉我在循环中使用ExecutorService而不是在循环中使用线程的优点吗?我的意思是,我不明白为什么这种方法比我现在使用的方法更好?你说过“退步是我不能重用这些线程”对吧。如果使用ExecutorService,则在工作完成后会自动释放线程,以便可以重用该线程。