source: java/main/src/main/java/com/framsticks/util/dispatching/Thread.java @ 98

Last change on this file since 98 was 98, checked in by psniegowski, 11 years ago

HIGHLIGHTS:

CHANGELOG:
Get data also on tree expansion.

Use nice framstick icon for empty nodes.

Update panel after reload if it is current.

Add shallow reload procedure.

Cut Gui prefix from several tree classes.

Bring back counter of GuiTreeNode?.

Use IdentityHashMap? were it is more appriopriate.

Remove TreeListener?.

Do not use TreeListener? in GUI.

Minor change.

Done migration to GuiTreeModel?.

BrowserTest? in that version always crashes frams.linux.

Move rendering implementation into GuiAbstractNode?.

Use hand-crafted list in GuiTreeNode?.

Generally, it would be a great place for WeakIdentityHashMap?
(but there is none in Java Collection Framework).

Remove superfluous logging.

Fix bug in GuiTreeNode?.

Use IdentityHashMap? instead of HashMap?.

Improve structure update.

Filter out invalid uids in UniqueListAccess?.

Improve TreeCellRenderer?.

Add filtering in TrackConsole?.

Improve TreeModel?.

More changes.

More improvements.

More changes.

Remove TreeNode?.

Support MetaNode? in the GuiTreeModel?.

Implement more in GuiTreeModel?.

Add CompositeParam? interface to FramsClass? and AccessInterface?.

Allow access by number to UniqueList?.

Add UidComparator?.

Use TreeMap? as a default accessee in unique list.

It keeps order of keys.

Introduce classes to use with new TreeModel?.

Another step.

Migrate from TreeNode? to Node in many places.

Remove some uses of TreeNode? as DefaultMutableTreeNode?.

Remove Path from TreeNode? interface.

Remove Path from TreeNode?.

Add Path recration from node feature.

Reworking TreeCellRenderer?.

Minor change of TreeOperations? interface.

Remove last methods from TreeNode?.

Another minor step.

Do not store reference to TreeAtFrame? in TreeNode?.

Add proxy exceptionHandler to StatusBar?.

Move panels management to TreeAtFrame?.

Store localChanges in the NodeAtFrame?.

More cleanup.

Move name computing to TreeCellRenderer?.

Move tooltip and icon computations to TreeCellRenderer?.

More dispatches removed.

Remove most dispatching from TreeNode?.

TreeNode? does not actually redispatch tasks.

Make Tree embedded in Browser use SwingDispatcher?.

Make lazy binding of Tree with Dispatcher.

Minor changes.

Organizational change in AbstractTree?.

Make AbstractTree? compose from Thread instead of inherit from it.

Make SwingDispatcher? and AtOnceDispatcher? Joinable compatible.

Add ListPanelProvider?.

Improve Controls readonly and enabled handling.

Properly pass ExceptionHandlers? in more places.

Make Tree.get accept ValueParam?.

  • This is to allow access number of list elements.

Remove not needed get redirection in ClientAtServer?.

Rename tryResolve to tryGet.

Unify tryResolveAndGet into tryResolve.

Remove resolveTop from Tree interface.

Make Tree.get accept Future<Path>.

Use get to implement resolveTop also in ObjectTree?.

Unify resolveTop and get in RemoteTree?.

Another minor step.

More minor changes in tree operations.

Minor organizational changes.

In RemoteTree? first fetch info for root.

Reworking resolving.

Minor changes.

Make ListAccess? return proxy iterators (instead of creating temporary collection).

Let AccessInterface? return Iterable<Param>.

Improve resolving.

More improvements.

First working completion in ManagedConsole?.

Rename resolve to resolveTop.

This reflects the actuall functionality.

Change semantic of tryResolve and tryResolveAndGet.

File size: 3.3 KB
Line 
1package com.framsticks.util.dispatching;
2
3import org.apache.log4j.Logger;
4
5import java.util.LinkedList;
6import java.util.ListIterator;
7
8
9import com.framsticks.params.annotations.ParamAnnotation;
10import com.framsticks.util.dispatching.RunAt;
11
12/**
13 * @author Piotr Sniegowski
14 */
15public class Thread<C> extends AbstractJoinable implements JoinableDispatcher<C> {
16
17        private static final Logger log = Logger.getLogger(Thread.class);
18
19        protected final java.lang.Thread thread;
20
21        private final LinkedList<Task<? extends C>> queue = new LinkedList<>();
22
23        public Thread() {
24                thread = new java.lang.Thread(new java.lang.Runnable() {
25                        @Override
26                        public void run() {
27                                Thread.this.routine();
28                        }
29                });
30        }
31
32
33        public Thread(java.lang.Thread thread) {
34                this.thread = thread;
35        }
36
37        @Override
38        protected void joinableStart() {
39                thread.start();
40        }
41
42        @Override
43        public final boolean isActive() {
44                return thread.equals(java.lang.Thread.currentThread());
45        }
46
47        protected void routine() {
48                log.debug("starting thread " + this);
49                assert getMonitor() != null;
50                ExceptionHandler exceptionHandler = getMonitor().getTaskExceptionHandler();
51                while (!java.lang.Thread.interrupted()) {
52                        Task<? extends C> task;
53                        synchronized (queue) {
54                                if (queue.isEmpty()) {
55                                        try {
56                                                queue.wait();
57                                        } catch (InterruptedException ignored) {
58                                                break;
59                                        }
60                                        continue;
61                                }
62                                task = queue.peekFirst();
63                                assert task != null;
64                                if (task.moment > System.currentTimeMillis()) {
65                                        try {
66                                                queue.wait(task.moment - System.currentTimeMillis());
67                                        } catch (InterruptedException ignored) {
68                                                continue;
69                                        }
70                                        continue;
71                                }
72                                queue.pollFirst();
73                        }
74                        try {
75                                task.runAt();
76                        } catch (Exception e) {
77                                if (exceptionHandler != null) {
78                                        if (exceptionHandler.handle(this, e)) {
79                                                continue;
80                                        }
81                                }
82                                log.error("error in thread: ", e);
83                        }
84                }
85                log.debug("finishing thread " + this);
86                finish();
87        }
88
89        protected void enqueueTask(Task<? extends C> task) {
90                synchronized (queue) {
91                        ListIterator<Task<? extends C>> i = queue.listIterator();
92                        while (i.hasNext()) {
93                                Task<? extends C> t = i.next();
94                                if (t.getMoment() > task.getMoment()) {
95                                        i.previous();
96                                        i.add(task);
97                                        task = null;
98                                        break;
99                                }
100                        }
101                        if (task != null) {
102                                queue.add(task);
103                        }
104
105                        /*
106                        Iterator<Task> j = queue.iterator();
107                        Task prev = null;
108                        while (j.hasNext()) {
109                                Task next = j.next();
110                                assert (prev == null) || prev.getMoment() <= next.getMoment();
111                                prev = next;
112                        }
113                        */
114                        queue.notify();
115                }
116        }
117
118        @Override
119        public void dispatch(final RunAt<? extends C> runnable) {
120                if (!(runnable instanceof Task)) {
121                        enqueueTask(new Task<C>(runnable) {
122                                @Override
123                                protected void runAt() {
124                                        runnable.runAt();
125                                }
126                        });
127                        return;
128                }
129                enqueueTask((Task<? extends C>) runnable);
130        }
131
132        @Override
133        protected void joinableInterrupt() {
134                thread.interrupt();
135        }
136
137        @Override
138        protected void joinableJoin() throws InterruptedException {
139                thread.join(500);
140                log.debug("joined " + this);
141        }
142
143        @ParamAnnotation
144        public void setName(String name) {
145                thread.setName(name);
146        }
147
148        @ParamAnnotation
149        public String getName() {
150                return thread.getName();
151        }
152
153        public static boolean interrupted() {
154                return java.lang.Thread.interrupted();
155        }
156
157        @Override
158        public String toString() {
159                return getName();
160        }
161
162        @Override
163        protected void joinableFinish() {
164        }
165
166}
Note: See TracBrowser for help on using the repository browser.