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

时间:2017-05-15 14:15:59

标签: android multithreading service

我想知道

  1. 如何不知道。由我的程序创建的线程,以便我可以很好地管理它们并根据需要停止。除了线程名称之外,有没有办法在Android Debug Monitor中识别程序中业务逻辑创建的线程?样本ADM输出如下:screenshot of Threads by name in Android Debug Monitor

  2. 如果创建线程的服务被破坏,甚至应用程序被明确杀死,则线程不会停止。线程仅在完成任务时停止,即如果没有明确中断则退出run()。

  3. 创建了一个示例程序来理解,如下所示:

    //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;
    }
    

    }

    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
    

0 个答案:

没有答案