匿名内部类线程的写法

来源:互联网 发布:ai软件基础教程视频 编辑:程序博客网 时间:2024/05/19 12:25

第一种

new Thread() {public void run() {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}}.start();

第二种

Thread thread = new Thread() {public void run() {try {Thread.sleep(1000);} catch (InterruptedException e) {}}};thread.start();

第三种

new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}}}).start();

例如

 bar = (ProgressBar)findViewById(R.id.myprogressbar);        Thread t = new Thread(){        public void run() {        int max = bar.getMax();        try {while(max!=bar.getProgress()){Thread.sleep(1000);int per = max/10;int cur = bar.getProgress();bar.setProgress(cur+per);}} catch (Exception e) {// TODO: handle exception}        }          };        t.start();




原创粉丝点击