Skip to content

Commit

Permalink
TaskManager: Refactor IoTask
Browse files Browse the repository at this point in the history
- Make IoTask a subclass of Task and remove the code of the special case.


git-svn-id: https://jedit.svn.sourceforge.net/svnroot/jedit/jEdit/trunk@22943 6b1eeb88-9816-0410-afa2-b43733a0f04e
  • Loading branch information
thomasmey committed Apr 22, 2013
1 parent ccff7d5 commit 8264485
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
18 changes: 18 additions & 0 deletions org/gjt/sp/jedit/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Vector;

import javax.swing.*;
import javax.swing.SwingWorker.StateValue;
import javax.swing.text.Segment;

import org.gjt.sp.jedit.browser.VFSBrowser;
Expand All @@ -42,6 +43,7 @@
import org.gjt.sp.jedit.buffer.JEditBuffer;
import org.gjt.sp.jedit.bufferio.BufferAutosaveRequest;
import org.gjt.sp.jedit.bufferio.BufferIORequest;
import org.gjt.sp.jedit.bufferio.IoTask;
import org.gjt.sp.jedit.bufferio.MarkersSaveRequest;
import org.gjt.sp.jedit.bufferset.BufferSet;
import org.gjt.sp.jedit.gui.DockableWindowManager;
Expand Down Expand Up @@ -720,6 +722,19 @@ public void setAutoReload(boolean value)
setFlag(AUTORELOAD, value);
autoreloadOverridden = isAutoreloadPropertyOverriden();
} //}}}
//
// //{{{ getIoTask() method
// public IoTask getIoTask()
// {
// return ioTask;
// } //}}}
//
// //{{{ setIoTask() method
// public void setIoTask(IoTask task)
// {
// assert(ioTask == null || ioTask != null && ioTask.getState() == StateValue.DONE);
// this.ioTask = task;
// } //}}}

//{{{ getAutoReloadDialog() method
/**
Expand Down Expand Up @@ -1841,6 +1856,9 @@ private boolean isAutoreloadPropertyOverriden()

private Socket waitSocket;
private List<BufferUndoListener> undoListeners;
//
// /** the current ioTask of this buffer */
// private volatile IoTask ioTask;
//}}}

//{{{ setPath() method
Expand Down
5 changes: 3 additions & 2 deletions org/gjt/sp/jedit/browser/VFSFileChooserDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.gjt.sp.jedit.io.*;
import org.gjt.sp.jedit.*;
import org.gjt.sp.jedit.buffer.JEditBuffer;
import org.gjt.sp.jedit.bufferio.IoTask;
import org.gjt.sp.util.*;
//}}}

Expand Down Expand Up @@ -583,7 +584,7 @@ public void valueUpdated(Task task)
} //}}}

//{{{ GetFileTypeRequest class
private class GetFileTypeRequest extends Task
private class GetFileTypeRequest extends IoTask
{
VFS vfs;
Object session;
Expand All @@ -593,7 +594,7 @@ private class GetFileTypeRequest extends Task
GetFileTypeRequest(VFS vfs, Object session,
String path, int[] type)
{
super(true);
super();
this.vfs = vfs;
this.session = session;
this.path = path;
Expand Down
6 changes: 3 additions & 3 deletions org/gjt/sp/jedit/bufferio/BufferIORequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@
import org.gjt.sp.jedit.io.EncodingServer;
import org.gjt.sp.util.IntegerArray;
import org.gjt.sp.util.SegmentBuffer;
import org.gjt.sp.util.Task;
//}}}

/**
* A buffer I/O request.
* @author Slava Pestov
* @version $Id$
*/
public abstract class BufferIORequest extends Task
public abstract class BufferIORequest extends IoTask
{
//{{{ Constants

Expand Down Expand Up @@ -97,14 +96,15 @@ public abstract class BufferIORequest extends Task
protected BufferIORequest(View view, Buffer buffer,
Object session, VFS vfs, String path)
{
super(true);
super();
this.view = view;
this.buffer = buffer;
this.session = session;
this.vfs = vfs;
this.path = path;

markersPath = Buffer.getMarkersPath(vfs, path);
//buffer.setIoTask(this);
} //}}}

//{{{ toString() method
Expand Down
3 changes: 2 additions & 1 deletion org/gjt/sp/jedit/io/VFSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.lang.reflect.InvocationTargetException;
import java.util.*;

import org.gjt.sp.jedit.bufferio.IoTask;
import org.gjt.sp.jedit.gui.ErrorListDialog;
import org.gjt.sp.jedit.msg.VFSUpdate;
import org.gjt.sp.jedit.*;
Expand Down Expand Up @@ -228,7 +229,7 @@ public static void runInAWTThread(Runnable run)
@Deprecated
public static void runInWorkThread(Task run)
{
if(!run.getIoTask())
if(!(run instanceof IoTask))
throw new IllegalArgumentException();

ThreadUtilities.runInBackground(run);
Expand Down
6 changes: 3 additions & 3 deletions org/gjt/sp/jedit/textarea/TextAreaTransferHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

//{{{ Imports
import org.gjt.sp.jedit.*;
import org.gjt.sp.jedit.bufferio.IoTask;
import org.gjt.sp.jedit.bufferset.BufferSetManager;
import org.gjt.sp.jedit.browser.VFSBrowser;
import org.gjt.sp.jedit.io.FileVFS;
import org.gjt.sp.jedit.io.VFS;
import org.gjt.sp.jedit.io.VFSManager;
import org.gjt.sp.util.AwtRunnableQueue;
import org.gjt.sp.util.Log;
import org.gjt.sp.util.Task;
import org.gjt.sp.util.ThreadUtilities;

import javax.swing.*;
Expand Down Expand Up @@ -484,14 +484,14 @@ private static class TextAreaSelection extends StringSelection
} //}}}

//{{{ DraggedURLLoader class
private static class DraggedURLLoader extends Task
private static class DraggedURLLoader extends IoTask
{
private final JEditTextArea textArea;
private final String url;

DraggedURLLoader(JEditTextArea textArea, String url)
{
super(true);
super();
this.textArea = textArea;
this.url = url;
}
Expand Down
14 changes: 1 addition & 13 deletions org/gjt/sp/util/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,15 @@ public abstract class Task implements Runnable, ProgressObserver
* It is set automatically when the task starts.
*/
private volatile Thread thread;
private final boolean ioTask;

private SwingWorker.StateValue state;
private volatile SwingWorker.StateValue state;

private volatile boolean cancellable = true;

//{{{ Task Constructor
protected Task()
{
this(false);
}

protected Task(boolean ioTask)
{
state = SwingWorker.StateValue.PENDING;
this.ioTask = ioTask;
} //}}}

//{{{ run() method
Expand Down Expand Up @@ -121,11 +114,6 @@ public long getMaximum()
return maximum;
}

public boolean getIoTask()
{
return ioTask;
}

public SwingWorker.StateValue getState()
{
return state;
Expand Down
32 changes: 12 additions & 20 deletions org/gjt/sp/util/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.gjt.sp.jedit.bufferio.IoTask;

/**
* The TaskManager manage Tasks in the Threadpool, it knows all of them, and
* sends events to TaskListeners.
Expand All @@ -39,14 +41,12 @@ public class TaskManager
private final List<TaskListener> listeners;

private final List<Task> tasks;
private final List<Task> ioTasks;
private final Object ioWaitLock;

private TaskManager()
{
listeners = new CopyOnWriteArrayList<TaskListener>();
tasks = Collections.synchronizedList(new ArrayList<Task>());
ioTasks = Collections.synchronizedList(new ArrayList<Task>());
ioWaitLock = new Object();
}

Expand All @@ -69,7 +69,13 @@ public int countTasks()
*/
public int countIoTasks()
{
return ioTasks.size();
int size = 0;
synchronized (tasks) {
for(Task task : tasks)
if(task instanceof IoTask)
size++;
}
return size;
}

public void addTaskListener(TaskListener listener)
Expand All @@ -90,10 +96,7 @@ public void removeTaskListener(TaskListener listener)

void fireWaiting(Task task)
{
if(task.getIoTask())
ioTasks.add(task);
else
tasks.add(task);
tasks.add(task);

List<TaskListener> listeners = this.listeners;
for (TaskListener listener : listeners)
Expand All @@ -113,18 +116,15 @@ void fireRunning(Task task)

void fireDone(Task task)
{
if(task.getIoTask())
ioTasks.remove(task);
else
tasks.remove(task);
tasks.remove(task);

List<TaskListener> listeners = this.listeners;
for (TaskListener listener : listeners)
{
listener.done(task);
}

if(task.getIoTask())
if(task instanceof IoTask)
{
AwtRunnableQueue.INSTANCE.queueAWTRunner(false);

Expand Down Expand Up @@ -214,14 +214,6 @@ public void cancelTasksByClass(Class<? extends Task> clazz)
task.cancel();
}
}
synchronized (ioTasks)
{
for(Task task: ioTasks)
{
if(task.getClass().equals(clazz))
task.cancel();
}
}
}

/**
Expand Down

0 comments on commit 8264485

Please sign in to comment.