Skip to content

Commit

Permalink
Refactored worker/concurrency classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sk89q committed Nov 20, 2013
1 parent b8ab4b1 commit bda9466
Show file tree
Hide file tree
Showing 26 changed files with 909 additions and 398 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/sk89q/skmcl/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

package com.sk89q.skmcl;

import com.sk89q.skmcl.launch.LaunchTask;
import com.sk89q.skmcl.concurrent.BackgroundExecutor;
import com.sk89q.skmcl.launch.LaunchWatcher;
import com.sk89q.skmcl.launch.LaunchWorker;
import com.sk89q.skmcl.profile.Profile;
import com.sk89q.skmcl.profile.ProfileManager;
import com.sk89q.skmcl.session.IdentityManager;
Expand All @@ -28,7 +29,6 @@
import com.sk89q.skmcl.util.Persistence;
import com.sk89q.skmcl.util.SharedLocale;
import com.sk89q.skmcl.util.SimpleLogFormatter;
import com.sk89q.skmcl.worker.Worker;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.java.Log;
Expand Down Expand Up @@ -80,13 +80,13 @@ public void hideLauncher() {
}
}

public void launchApplication(Window owner, Worker worker, Profile profile) {
public void launchApplication(Window owner, BackgroundExecutor executor, Profile profile) {
profile.setLastLaunchDate(new Date());
Persistence.commitAndForget(profile);
getProfiles().notifyUpdate();

LaunchTask task = new LaunchTask(profile);
LaunchWatcher watcher = new LaunchWatcher(this, worker.submit(task));
LaunchWorker task = new LaunchWorker(profile);
LaunchWatcher watcher = new LaunchWatcher(this, executor.submit(task));
new Thread(watcher).start();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/sk89q/skmcl/application/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

package com.sk89q.skmcl.application;

import com.sk89q.skmcl.concurrent.AbstractWorker;
import com.sk89q.skmcl.launch.LaunchContext;
import com.sk89q.skmcl.launch.LaunchedProcess;
import com.sk89q.skmcl.worker.Task;

import java.io.IOException;

Expand All @@ -41,7 +41,7 @@ public interface Instance {
*
* @return an update worker
*/
Task<Instance> getUpdater();
AbstractWorker<Instance> getUpdater();

/**
* Launch this instance.
Expand Down
164 changes: 164 additions & 0 deletions src/main/java/com/sk89q/skmcl/concurrent/AbstractExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* SK's Minecraft Launcher
* Copyright (C) 2010, 2011 Albert Pham <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.skmcl.concurrent;

import lombok.NonNull;
import lombok.extern.java.Log;

import javax.swing.event.EventListenerList;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;

@Log
abstract class AbstractExecutor extends Observable implements ProgressObservable, Observer {

private final ExecutorService executor;
private final EventListenerList listenerList = new EventListenerList();

/**
* Create a new background executor with the given executor.
*
* @param executor the executor
*/
public AbstractExecutor(@NonNull ExecutorService executor) {
this.executor = executor;
}

/**
* Create a new background executor using a default executor.
*/
public AbstractExecutor() {
this(Executors.newCachedThreadPool());
}

/**
* Add a given task to the list of tasksin execution
*
* @param task the task
*/
protected abstract void addTask(Task<?> task);

/**
* Remove a given task from the list of tasks in execution.
*
* @param task the task
*/
protected abstract void removeTask(Task<?> task);

/**
* Submit a worker to be executed and tracked by this class.
*
* @param worker the worker
* @param <V> the return value type
* @return a task
*/
protected <V> Task<V> submit(Worker<V> worker) {
Task<V> task = new Task<V>(worker);
WorkerWrapper<V> wrapper = new WorkerWrapper<V>(worker, task);
addTask(task);
task.setFuture(executor.submit(wrapper));
return task;
}

/**
* Add an executor listener to this instance.
*
* @param l the listener
*/
public void addExecutorListener(ExecutorListener l) {
listenerList.add(ExecutorListener.class, l);
}

/**
* Remove the executor listener from this instance.
* * @param l the listener
*/
public void removeExecutorListener(ExecutorListener l) {
listenerList.remove(ExecutorListener.class, l);
}

/**
* Fire an event for an exception that has occurred in a worker.
*
* @param throwable the error
*/
private void fireException(Throwable throwable) {
ExceptionEvent event = null;
Object[] listeners = listenerList.getListenerList();
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ExecutorListener.class) {
if (event == null)
event = new ExceptionEvent(this, throwable);
((ExecutorListener) listeners[i + 1]).exceptionThrown(event);
}
}
}

@Override
public void update(Observable o, Object arg) {
setChanged();
notifyObservers();
}

/**
* Manage a worker so that any errors that occur during execution can be reported to
* the underlying {@link BackgroundExecutor}.
*
* @param <V> the return value type
*/
private class WorkerWrapper<V> implements Callable<V> {
private final Worker<V> worker;
private final Task<V> task;

/**
* Create a new instance of this wrapper.
*
* @param worker the worker
* @param task the task
*/
private WorkerWrapper(Worker<V> worker, Task<V> task) {
this.worker = worker;
this.task = task;
}

@Override
public V call() throws Exception {
try {
log.log(Level.INFO, "Executing {0}...", worker);
return worker.call();
} catch (InterruptedException e) {
throw e;
} catch (Exception e) {
fireException(e);
throw e;
} catch (Throwable t) {
fireException(t);
throw new RuntimeException(t);
} finally {
removeTask(task);
log.log(Level.INFO, "{0} is done", worker);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.skmcl.worker;
package com.sk89q.skmcl.concurrent;

import java.util.concurrent.Callable;

public abstract class Task<V> extends Segment implements Callable<V> {
public abstract class AbstractWorker<V> extends WorkUnit implements Worker<V> {

@Override
public V call() throws Exception {
Expand Down
148 changes: 148 additions & 0 deletions src/main/java/com/sk89q/skmcl/concurrent/BackgroundExecutor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* SK's Minecraft Launcher
* Copyright (C) 2010, 2011 Albert Pham <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.sk89q.skmcl.concurrent;

import lombok.NonNull;
import lombok.extern.java.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

/**
* A convenient executor for {@link Worker}s.
*/
@Log
public class BackgroundExecutor extends AbstractExecutor {

private final List<Task<?>> inProgress = new ArrayList<Task<?>>();

public BackgroundExecutor(@NonNull ExecutorService executor) {
super(executor);
}

public BackgroundExecutor() {
}

/**
* Returns the total number of tasks that are currently running.
*
* @return the task count
*/
public synchronized int getActiveTaskCount() {
return inProgress.size();
}

@Override
protected void addTask(Task<?> task) {
synchronized (this) {
inProgress.add(task);
setChanged();
notifyObservers();
}

task.addObserver(this);
}

@Override
protected void removeTask(Task<?> task) {
synchronized (this) {
inProgress.remove(task);
setChanged();
notifyObservers();
}
}

/**
* Submit a worker to be executed and tracked by this class.
*
* @param worker the worker
* @param <V> the return value type
* @return a task
*/
@Override
public synchronized <V> Task<V> submit(Worker<V> worker) {
return super.submit(worker);
}

/**
* Interrupt all currently running tasks.
* <p/>
* <p>This <code>BackgroundExecutor</code> can be re-used even after this
* method is called.</p>
*/
public synchronized void cancelAll() {
for (Task<?> task : inProgress) {
task.cancel(true);
}

inProgress.clear();
setChanged();
notifyObservers();
}

@Override
public synchronized double getProgress() {
if (inProgress.isEmpty()) {
return -1;
} else {
double progress = 0;

for (ProgressObservable w : inProgress) {
double p = w.getProgress();
if (p < 0) {
return -1;
}
progress += p / inProgress.size();
}

return progress;
}
}

@Override
public synchronized String getLocalizedTitle() {
if (inProgress.isEmpty()) {
return null;
} else {
return inProgress.get(0).getLocalizedTitle();
}
}

@Override
public synchronized String getLocalizedStatus() {
if (inProgress.isEmpty()) {
return null;
} else {
return inProgress.get(0).getLocalizedStatus();
}
}

@Override
public synchronized boolean shouldConfirmInterrupt() {
for (ProgressObservable w : inProgress) {
if (w.shouldConfirmInterrupt()) {
return true;
}
}

return false;
}

}
Loading

0 comments on commit bda9466

Please sign in to comment.