1. 程式人生 > >匿名內部類執行緒的寫法

匿名內部類執行緒的寫法

第一種

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() {			
			@Override
			public 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();