Android多线程操作,as快捷键笔记

Android studio 快捷键

 cmd+p 快速查看该方法的参数定义
*
* option + shift +上下 快速移动上下行
*
* cmd + e 显示最近操作的文件
*
* cmd + option +左右 回到之前浏览过的地方
* option + shift +点击 多光标操作
* control +g 多重选择
* f3  添加书签
* cmd +f3 查看书签
*
* cmd + y 在当前页面预览代码
* cmd + option + t 对某行代码进行重构,例如增加if 语句
*
* option + enter 快速提示

 

  • 多线程操作不要显式调用Thread,使用AsyncTask或者ThreadPoolExecutor

          //执行task
    //        new DownLoadTask().execute();
      
        class DownLoadTask extends AsyncTask<Void,Integer,Boolean>{
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Boolean doInBackground(Void... voids) {
                //后台处理返回处理结果
                //更新处理进度
                publishProgress(11);
                return null;
            }
    
            @Override
            protected void onProgressUpdate(Integer... values) {
               //进度处理
            }
    
            @Override
            protected void onPostExecute(Boolean aBoolean) {
                //结果处理
            }
        }
    

     

    private void userThreadPoolExecutor(){
            int NUMBER_CORES = Runtime.getRuntime().availableProcessors();
            int keep_alive = 8;
    
            BlockingQueue que = new LinkedBlockingDeque();//队列,这里面的任务可以放入int的max个
    
            ArrayBlockingQueue arrque = new ArrayBlockingQueue(2);//只放入两个任务
    //
            ThreadPoolExecutor executor = new ThreadPoolExecutor(2,5,keep_alive,
                    TimeUnit.SECONDS,arrque,new NameTreadFactory(),new MyIgnorePolicy());
    
            for (int i = 0;i < 10;i++){
                executor.execute(new MyRunable(""+i));
            }
    
    
        }
        static class NameTreadFactory implements ThreadFactory {
    
            private final AtomicInteger mThreadNum = new AtomicInteger(1);
    
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r, "my-thread-" + mThreadNum.getAndIncrement());
                System.out.println(t.getName() + " has been created");
                return t;
            }
        }
        public static class MyIgnorePolicy implements RejectedExecutionHandler {
    
            public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
                doLog(r, e);
            }
    
            private void doLog(Runnable r, ThreadPoolExecutor e) {
                // 可做日志记录等
                Log.d("tag",r.toString() + " rejected");
    //          System.out.println("completedTaskCount: " + e.getCompletedTaskCount());
            }
        }
        class  MyRunable implements Runnable {
            private String name;
    
            public MyRunable(String name){
                    this.name = name;
            }
    
            @Override
            public void run() {
                try {
                    System.out.println(this.toString() + " is running!");
                    Thread.sleep(3000); //让任务执行慢点
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            public String getName() {
                return name;
            }
    
            @Override
            public String toString() {
                return "My task---[name==="+name+" ]";
            }
        }

     

     

    
    










posted @ 2019-03-30 15:18  菜鸟工程司  阅读(279)  评论(0编辑  收藏  举报