Skip to content

Instantly share code, notes, and snippets.

@jeeeyul
Created February 14, 2013 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeeeyul/4949733 to your computer and use it in GitHub Desktop.
Save jeeeyul/4949733 to your computer and use it in GitHub Desktop.
AsyncJob.java
package net.jeeeyul.eclipse.common.ui.progress;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Display;
public abstract class AsyncJob<R> extends Job {
private Display display;
private R result;
private Throwable error;
public AsyncJob(Display display, String name) {
super(name);
this.display = display;
}
public AsyncJob(String name) {
super(name);
}
private Display getDisplay() {
if (display != null) {
return display;
} else {
return Display.getDefault();
}
}
/**
* {@link #runInBackgroundThread(IProgressMonitor)} 작업 수행중 에러가 발생한 경우 처리합니다.
* 클라이언트는 이 메서드를 오버라이드 하여 에러를 처리 해야 합니다. 이 메서드는 UI 스레드에서 실행됩니다.
*
* @param t
* 발생한 예외.
*/
protected abstract void handleErrorInUIThread(Throwable t);
/**
* 작업이 성공적으로 완료된 후 콜백되며, 이 메서드는 UI 스레드에서 실행됩니다.
*
* @param result
* {@link #runInBackgroundThread(IProgressMonitor)}가 리턴한 작업 결과.
*/
protected abstract void handleResultInUIThread(R result);
@Override
protected final IStatus run(IProgressMonitor monitor) {
result = null;
error = null;
try {
result = runInBackgroundThread(monitor);
} catch (Throwable e) {
error = e;
}
if (error == null) {
getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
handleResultInUIThread(result);
}
});
} else {
getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
handleErrorInUIThread(error);
}
});
}
return Status.OK_STATUS;
}
/**
* 배경 스레드에서 실행될 주 작업을 기술합니다. 작업 결과 처리는
* {@link #handleResultInUIThread(Object)} 에서 기술 합니다.
*
* @param m
* 프로그래스 모니터.
* @return 작업 결과.
*/
protected abstract R runInBackgroundThread(IProgressMonitor m);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment