쓰레드가 일단 보이는 것만 해도 두개임
doInBackground 하나
onProgressUpdate 하나
반복문은 반복문대로 돌아가고 화면은 화면대로 바꾸고
이것이 멀티쓰레드
어썸-
class IncreaseTask extends AsyncTask<Void, Integer, Void> {
// 멀티 쓰레드로 하고 싶은 일
public Void doInBackground(Void... params) { // Void 없다 / ... 배열
for (int i = 0; i < 100; ) {
this.publishProgress(i); // onProgressUpdate를 호출(멀티쓰레드) / 호출해놓고 기달 ㄴㄴ 반복분은 계속 돈다.
try {
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
if (i >= 100) {
i = 0;
}
i = i+10;
}
return null;
// 위젯 모양 바꿀 일
public void onProgressUpdate(Integer... param) { // ...은 배열을 의미함
progressBar.setProgress(param[0]);
editText.append(progressBar.getProgress() + " \n");
}
}
}
사용 하는 법
클래스를 만들었으면 사용할 곳에서 객체를 생성해줘야한다
IncreaseTask task = new IncreaseTask();
task.execute(); // IncreaseTask 안에 doInBackground 메소드(멀티쓰레드)가 시작된다.