Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何计算android程序创建的线程数_Android_Multithreading_Service - Fatal编程技术网

如何计算android程序创建的线程数

如何计算android程序创建的线程数,android,multithreading,service,Android,Multithreading,Service,我想知道 如何知道我的程序创建的线程数,以便我能够很好地管理它们并根据需要停止。除了线程的名称之外,还有什么方法可以在Android Debug Monitor的程序中识别由业务逻辑创建的线程吗?ADM输出示例如下: 如果创建线程的服务被销毁,甚至应用程序被明确终止,线程不会停止。线程仅在完成任务时停止,即如果没有显式中断,则在退出run()时停止 创建了一个示例程序,以便理解,如下所示: //DemoService.java class Intent mIntent; String TAG=

我想知道

  • 如何知道我的程序创建的线程数,以便我能够很好地管理它们并根据需要停止。除了线程的名称之外,还有什么方法可以在Android Debug Monitor的程序中识别由业务逻辑创建的线程吗?ADM输出示例如下:

  • 如果创建线程的服务被销毁,甚至应用程序被明确终止,线程不会停止。线程仅在完成任务时停止,即如果没有显式中断,则在退出run()时停止

  • 创建了一个示例程序,以便理解,如下所示:

    //DemoService.java class
    Intent mIntent;
    
    String TAG="Logs";
    
    Thread myThread;
    
    static int k = 0;
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG,"--------------service started'");
        mIntent = intent;
        final int currentId = startId;
    
        k++;
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
    
                for (int i = 0; i < 5; i++)
                {
                    long endTime = System.currentTimeMillis() + 5*1000;
    
                    while (System.currentTimeMillis() < endTime) {
                        synchronized (this) {
                            try {
                                wait(endTime -
                                        System.currentTimeMillis());
    
                                int[] numbers = mIntent.getIntArrayExtra("numbers");
                                int result = numbers[0] + numbers[1];
    
                                Log.d(TAG,"result : " + result);
    
                            } catch (Exception e) {
                            }
                        }
                    }
                    Log.i(TAG, "Service running " + currentId);
                }
    
            }
        };
    
        myThread = new Thread(myRunnable);
        myThread.start();
        myThread.setName("Test Thread  " + k);
    
        Log.d(TAG, "Thread Id : " + myThread.getId() + ", " + "Name : " + "Test Thread  " + k);
    
        checkThreads();
        return Service.START_STICKY;
    }
    
    private void checkThreads(){
        Log.d(TAG,"myThread.getName(): " + myThread.getName() + ", " + "myThread.isAlive(): " + myThread.isAlive());
    
    }
    
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    }

    调用stopService()显式停止服务时的输出
    05-15 19:05:56.020 30097-30097/? D/日志:------------服务已启动'
    05-15 19:05:56.022 30097-30097/? D/日志:线程Id:878,名称:测试线程1
    05-15 19:05:56.022 30097-30097/? D/Logs:myThread.getName():测试线程1,myThread.isAlive():true
    05-15 19:06:01.023 30097-31250/? D/记录:结果:77
    05-15 19:06:01.023 30097-31250/? I/Logs:服务正在运行1
    05-15 19:06:02.283 30097-30097/? D/日志:------------服务已启动'
    05-15 19:06:02.284 30097-30097/? D/日志:线程Id:879,名称:测试线程2
    05-15 19:06:02.284 30097-30097/? D/Logs:myThread.getName():测试线程2,myThread.isAlive():true
    05-15 19:06:03.004 30097-30097/? D/日志:------------服务已启动'
    05-15 19:06:03.005 30097-30097/? D/日志:线程Id:880,名称:测试线程3
    05-15 19:06:03.005 30097-30097/? D/Logs:myThread.getName():测试线程3,myThread.isAlive():true
    05-15 19:06:06.024 30097-31250/? D/记录:结果:77
    05-15 19:06:06.024 30097-31250/? I/Logs:服务正在运行1
    05-15 19:06:07.286 30097-31258/? D/记录:结果:77
    05-15 19:06:07.286 30097-31258/? I/Logs:正在运行的服务2
    05-15 19:06:08.007 30097-31259/? D/记录:结果:77
    05-15 19:06:08.007 30097-31259/? I/Logs:服务正在运行3
    05-15 19:06:11.024 30097-31250/? D/记录:结果:77
    05-15 19:06:11.024 30097-31250/? I/Logs:服务正在运行1
    05-15 19:06:12.286 30097-31258/? D/记录:结果:77
    05-15 19:06:12.287 30097-31258/? I/Logs:正在运行的服务2
    05-15 19:06:13.007 30097-31259/? D/记录:结果:77
    05-15 19:06:13.007 30097-31259/? I/Logs:服务正在运行3
    05-15 19:06:16.025 30097-31250/? D/记录:结果:77
    05-15 19:06:16.025 30097-31250/? I/Logs:服务正在运行1
    05-15 19:06:17.288 30097-31258/? D/记录:结果:77
    05-15 19:06:17.288 30097-31258/? I/Logs:正在运行的服务2
    05-15 19:06:18.007 30097-31259/? D/记录:结果:77
    05-15 19:06:18.007 30097-31259/? I/Logs:服务正在运行3
    05-15 19:06:21.025 30097-31250/? D/记录:结果:77
    05-15 19:06:21.025 30097-31250/? I/Logs:服务正在运行1
    
    05-15 19:06:21.537 30097-30097/? D/Logs:停止服务线程在进程/VM终止时停止。如果您启动线程,您有责任管理它们,最好是保留对它们的引用并清理它们。如果您不想自己做这件事,这里有
    ExecutorService
    。谢谢您的回复。这是关于线程行为的。但是,即使在调用stopservice()之后,我的服务也不会停止。我想知道服务会一直执行到线程执行为止,正如您在输出中看到的那样。那么,使用Intent Service(表示不需要调用stopService(),因为任务完成后两者都会停止)有什么区别呢?另外,对于第一个问题,我明白跟踪线程总数并没有帮助。相反,只要在代码中创建了新线程,就应该进行适当的处理来管理线程或de
    public class MainActivity extends AppCompatActivity {
    
    EditText num1,num2;
    
    private String TAG = "Logs";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        num1 = (EditText) findViewById(R.id.num1);
        num2 = (EditText) findViewById(R.id.num2);
    
    }
    
    public void onClickAdd(View view){
        if(view.getId() == R.id.add_btn){
            Intent serviceIntent = new Intent();
            serviceIntent.setClass(MainActivity.this, DemoService.class);
    
            int[] numbers = new int[2];
    
            numbers[0]= Integer.parseInt(num1.getText().toString());
            numbers[1]=Integer.parseInt(num2.getText().toString());
            serviceIntent.putExtra("numbers",numbers);
            startService(serviceIntent);
        }
    
    }
    
    public void onClickServiceStop(View view){
        Intent serviceIntent = new Intent();
        serviceIntent.setClass(MainActivity.this, DemoService.class);
    
        Log.d(TAG,"stopping service");
        stopService(serviceIntent);
    }
    
    Output when service is stopped explicitly with a call to stopService()
    
    05-15 19:05:56.020 30097-30097/? D/Logs: --------------service started'
    05-15 19:05:56.022 30097-30097/? D/Logs: Thread Id : 878, Name : Test Thread  1
    05-15 19:05:56.022 30097-30097/? D/Logs: myThread.getName(): Test Thread  1, myThread.isAlive(): true
    05-15 19:06:01.023 30097-31250/? D/Logs: result : 77
    05-15 19:06:01.023 30097-31250/? I/Logs: Service running 1
    05-15 19:06:02.283 30097-30097/? D/Logs: --------------service started'
    05-15 19:06:02.284 30097-30097/? D/Logs: Thread Id : 879, Name : Test Thread  2
    05-15 19:06:02.284 30097-30097/? D/Logs: myThread.getName(): Test Thread  2, myThread.isAlive(): true
    05-15 19:06:03.004 30097-30097/? D/Logs: --------------service started'
    05-15 19:06:03.005 30097-30097/? D/Logs: Thread Id : 880, Name : Test Thread  3
    05-15 19:06:03.005 30097-30097/? D/Logs: myThread.getName(): Test Thread  3, myThread.isAlive(): true
    05-15 19:06:06.024 30097-31250/? D/Logs: result : 77
    05-15 19:06:06.024 30097-31250/? I/Logs: Service running 1
    05-15 19:06:07.286 30097-31258/? D/Logs: result : 77
    05-15 19:06:07.286 30097-31258/? I/Logs: Service running 2
    05-15 19:06:08.007 30097-31259/? D/Logs: result : 77
    05-15 19:06:08.007 30097-31259/? I/Logs: Service running 3
    05-15 19:06:11.024 30097-31250/? D/Logs: result : 77
    05-15 19:06:11.024 30097-31250/? I/Logs: Service running 1
    05-15 19:06:12.286 30097-31258/? D/Logs: result : 77
    05-15 19:06:12.287 30097-31258/? I/Logs: Service running 2
    05-15 19:06:13.007 30097-31259/? D/Logs: result : 77
    05-15 19:06:13.007 30097-31259/? I/Logs: Service running 3
    05-15 19:06:16.025 30097-31250/? D/Logs: result : 77
    05-15 19:06:16.025 30097-31250/? I/Logs: Service running 1
    05-15 19:06:17.288 30097-31258/? D/Logs: result : 77
    05-15 19:06:17.288 30097-31258/? I/Logs: Service running 2
    05-15 19:06:18.007 30097-31259/? D/Logs: result : 77
    05-15 19:06:18.007 30097-31259/? I/Logs: Service running 3
    05-15 19:06:21.025 30097-31250/? D/Logs: result : 77
    05-15 19:06:21.025 30097-31250/? I/Logs: Service running 1
    05-15 19:06:21.537 30097-30097/? D/Logs: stopping service    <----- Service stopped explcitly with stopService()
    05-15 19:06:22.288 30097-31258/? D/Logs: result : 77
    05-15 19:06:22.288 30097-31258/? I/Logs: Service running 2
    05-15 19:06:23.008 30097-31259/? D/Logs: result : 77
    05-15 19:06:23.008 30097-31259/? I/Logs: Service running 3
    05-15 19:06:27.288 30097-31258/? D/Logs: result : 77
    05-15 19:06:27.289 30097-31258/? I/Logs: Service running 2
    05-15 19:06:28.008 30097-31259/? D/Logs: result : 77
    05-15 19:06:28.008 30097-31259/? I/Logs: Service running 3
    
    
    Output when app is killed explicitly, thread and service continues to run
    05-15 19:08:35.093 30097-30097/? D/Logs: --------------service started'
    05-15 19:08:35.094 30097-30097/? D/Logs: Thread Id : 882, Name : Test Thread  4
    05-15 19:08:35.094 30097-30097/? D/Logs: myThread.getName(): Test Thread  4, myThread.isAlive(): true
    05-15 19:08:38.382 30097-30097/? D/Logs: --------------service started'
    05-15 19:08:38.382 30097-30097/? D/Logs: Thread Id : 883, Name : Test Thread  5
    05-15 19:08:38.382 30097-30097/? D/Logs: myThread.getName(): Test Thread  5, myThread.isAlive(): true
    05-15 19:08:38.828 30097-30097/? D/Logs: --------------service started'
    05-15 19:08:38.829 30097-30097/? D/Logs: Thread Id : 884, Name : Test Thread  6
    05-15 19:08:38.829 30097-30097/? D/Logs: myThread.getName(): Test Thread  6, myThread.isAlive(): true
    05-15 19:08:40.096 30097-31891/? D/Logs: result : 77
    05-15 19:08:40.096 30097-31891/? I/Logs: Service running 1
    05-15 19:08:43.384 30097-31974/? D/Logs: result : 77
    05-15 19:08:43.384 30097-31974/? I/Logs: Service running 2
    05-15 19:08:43.830 30097-31981/? D/Logs: result : 77
    05-15 19:08:43.830 30097-31981/? I/Logs: Service running 3
    05-15 19:08:45.097 30097-31891/? D/Logs: result : 77
    05-15 19:08:45.097 30097-31891/? I/Logs: Service running 1
    05-15 19:08:48.426 30097-31974/? D/Logs: result : 77
    05-15 19:08:48.426 30097-31974/? I/Logs: Service running 2
    05-15 19:08:48.868 30097-31981/? D/Logs: result : 77
    05-15 19:08:48.868 30097-31981/? I/Logs: Service running 3
    05-15 19:08:50.137 30097-31891/? D/Logs: result : 77
    05-15 19:08:50.137 30097-31891/? I/Logs: Service running 1
    05-15 19:08:53.467 30097-31974/? D/Logs: result : 77
    05-15 19:08:53.467 30097-31974/? I/Logs: Service running 2
    05-15 19:08:53.908 30097-31981/? D/Logs: result : 77
    05-15 19:08:53.908 30097-31981/? I/Logs: Service running 3
    05-15 19:08:55.176 30097-31891/? D/Logs: result : 77
    05-15 19:08:55.176 30097-31891/? I/Logs: Service running 1
    05-15 19:08:58.508 30097-31974/? D/Logs: result : 77
    05-15 19:08:58.508 30097-31974/? I/Logs: Service running 2
    05-15 19:08:58.948 30097-31981/? D/Logs: result : 77
    05-15 19:08:58.948 30097-31981/? I/Logs: Service running 3
    05-15 19:09:00.218 30097-31891/? D/Logs: result : 77
    05-15 19:09:00.218 30097-31891/? I/Logs: Service running 1
    05-15 19:09:03.548 30097-31974/? D/Logs: result : 77
    05-15 19:09:03.548 30097-31974/? I/Logs: Service running 2
    05-15 19:09:03.985 30097-31981/? D/Logs: result : 77
    05-15 19:09:03.985 30097-31981/? I/Logs: Service running 3