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

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

HIGHLIGHTS:

  • add <include/> to configuration
  • add side notes to tree
    • used to store arbitrary information alongside the tree structure
  • migrate to log4j2
    • supports lazy string evaluation of passed arguments
  • improve GUI tree
    • it stays in synchronization with actual state (even in high load test scenario)
  • improve panel management in GUI
  • make loading objects in GUI more lazy
  • offload parsing to connection receiver thread
    • info parsing
    • first step of objects parsing
  • fix connection parsing bug (eof in long values)
  • support zero-arguments procedure in table view

CHANGELOG:
Implement procedure calls from table view.

Refactorization around procedures in tables.

Add table editor for buttons.

Render buttons in the the list view.

Further improve Columns.

Add Column class for TableModel?.

Accept also non-arguments ProcedureParams? in tableView.

Increase maximal TextAreaControl? size.

Add tooltip to ProcedureControl?.

Fix bug of interpreting eofs in long values by connection reader.

Further rework connection parsing.

Simplify client connection processing.

Test ListChange? modification.

Test ListChange? events with java server.

Add TestChild?.

Fix bug with fast deregistering when connecting to running server.

Another minor refactorization in TreeOperations?.

Fix bug in SimpleAbstractAccess? loading routine.

Another minor improvement.

Minor change.

Make reading of List objects two-phase.

Another minor change.

Dispatch parsing into receiver thread.

Another step.

Enclose passing value in ObjectParam? case in closure.

Minor step.

Minor change on way to offload parsing.

Temporarily comment out single ValueParam? get.

It will be generalized to multi ValueParam?.

Process info in receiver thread.

Add DispatchingExceptionHandler?.

Make waits in browser test longer.

Use FETCHED_MARK.

It is honored in GUI, where it used to decide whether to get values

after user action.

It is set in standard algorithm for processing fetched values.

Add remove operation to side notes.

Make loading more lazy.

Improve loading policy.

On node choose load itself, on node expansion, load children.

Minor improvement.

Fix bug with panel interleaving.

Minor improvements.

Improve panel management.

More cleaning around panels.

Reorganize panels.

Further improve tree.

Fix bug in TreeModel?.

Remove children from TreeNode?.

Implement TreeNode? hashCode and equals.

Make TreeNode? delegate equals and hashcode to internal reference.

Move listeners from TreeNode? to side notes.

Store path.textual as a side note.

Side note params instead of accesses for objects.

More refactorizations.

In TreeNode? bindAccess based on side notes.

Minor step.

Hide createAccess.

Rename AccessInterface? to Access.

Minor changes.

Several improvements in high load scenarios.

Change semantics of ArrayListAccess?.set(index, null);

It now removes the element, making list shorter
(it was set to null before).

Add path remove handler.

Handle exceptions in Connection.

Update .gitignore

Configure logging to file.

Move registration to TreeModel?.

Further refactorization.

Minor refactorization.

Minor improvements.

Use specialized event also for Modify action of ListChange?.

Use remove events.

Use the insertion events for tree.

Further improve tree refreshing.

Further improve reacting on events in GUI.

Fix problem with not adding objects on addition list change.

Migrate to log4j lazy String construction interface.

Migrate imports to log4j2.

Drop dependency on adapter to version 1.2.

Switch log4j implementation to log4j2.

Add dirty mark to the NodeAtFrame?.

Make selecting in AccessInterfaces? type safe.

Ignore containers size settings in Model and Genotype.

Use tree side notes to remember local changes and panels.

Add sideNotes to tree.

They will be used to store various accompanying information
right in the tree.

Use ReferenceIdentityMap? from apache in TreeNode?.

It suits the need perfectly (weak semantics on both key and value).

Make ArrayListParam? do not react size changes.

Guard in TableModel? before not yet loaded objects.

Add <include/> clause and AutoInjector?.

Extract common columns configuration to separate xml,
that can be included by other configurations.

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