1414import notepack .app .task .TypeGui ;
1515import notepack .app .task .TypeNote ;
1616import notepack .app .task .TypeNotepad ;
17+ import java .util .concurrent .ExecutorService ;
18+ import java .util .concurrent .Executors ;
19+ import java .util .concurrent .TimeUnit ;
20+ import java .util .concurrent .atomic .AtomicInteger ;
21+ import java .util .concurrent .atomic .AtomicReference ;
22+ import notepack .app .domain .exception .MessageError ;
1723
1824public class MessageBus {
1925
@@ -24,13 +30,17 @@ public class MessageBus {
2430
2531 private Thread dispatchThread ;
2632 private boolean dispatcherStop = false ;
33+ private final AtomicInteger tasksActive = new AtomicInteger ();
34+ private int maxTasksActive = 3 ;
2735
2836 public MessageBus () {
2937 tasks = new ConcurrentLinkedQueue <>();
3038
3139 noteListeners = new ArrayList <>();
3240 notepadListeners = new ArrayList <>();
3341 guiListeners = new ArrayList <>();
42+
43+ tasksActive .set (0 );
3444 }
3545
3646 public void startDispatcher () {
@@ -53,7 +63,7 @@ public void run() {
5363 tasks .add (new ShowUserMessage (e .getMessage (), ShowUserMessage .TYPE .ERROR ));
5464 }
5565 try {
56- Thread .sleep (10 );
66+ Thread .sleep (100 );
5767 } catch (InterruptedException ex ) {
5868 break ;
5969 }
@@ -70,38 +80,61 @@ public void stopDispatcher() {
7080 dispatchThread = null ;
7181 }
7282
83+ synchronized private Task getTaskToDispatch () {
84+ return tasks .poll ();
85+ }
86+
7387 private void dispatch () throws MessageError {
7488
75- for (Task t : tasks ) {
76-
77- if (t instanceof BaseTask ) {
78- ((BaseTask ) t ).setMessageBus (this );
79- }
80-
81- tasks .remove (t );
89+ int currentTasksCounter = tasksActive .get ();
90+ if (currentTasksCounter > maxTasksActive ) {
91+ return ;
92+ }
8293
83- if (t instanceof TypeNote ) {
84- t .dispatch ();
94+ Task t = getTaskToDispatch ();
95+ if (t == null ) {
96+ return ;
97+ }
98+ if (t instanceof BaseTask ) {
99+ ((BaseTask ) t ).setMessageBus (this );
100+ }
85101
86- for (NoteListener l : noteListeners ) {
87- ((TypeNote ) t ).notify (l );
88- }
89- }
90- if (t instanceof TypeNotepad ) {
91- t .dispatch ();
102+ Thread job = new Thread (new Runnable () {
103+ @ Override
104+ public void run () {
105+ tasksActive .incrementAndGet ();
92106
93- for (NotepadListener l : notepadListeners ) {
94- ((TypeNotepad ) t ).notify (l );
95- }
96- }
97- if (t instanceof TypeGui ) {
98- for (GuiListener l : guiListeners ) {
99- l .proceed ((TypeGui ) t );
107+ try {
108+ if (t instanceof TypeNote ) {
109+ t .dispatch ();
110+
111+ for (NoteListener l : noteListeners ) {
112+ ((TypeNote ) t ).notify (l );
113+ }
114+ }
115+ if (t instanceof TypeNotepad ) {
116+ t .dispatch ();
117+
118+ for (NotepadListener l : notepadListeners ) {
119+ ((TypeNotepad ) t ).notify (l );
120+ }
121+ }
122+ if (t instanceof TypeGui ) {
123+ for (GuiListener l : guiListeners ) {
124+ l .proceed ((TypeGui ) t );
125+ }
126+ }
127+ } catch (MessageError ex ) {
128+ Logger .getLogger (MessageBus .class .getName ()).log (Level .SEVERE , null , ex );
129+ tasks .add (new ShowUserMessage (ex .getMessage (), ShowUserMessage .TYPE .ERROR ));
100130 }
101- }
102131
103- }
132+ tasksActive .decrementAndGet ();
133+ }
104134
135+ });
136+ job .setName ("Task processing " + t .toString ());
137+ job .start ();
105138 }
106139
107140 public void registerNoteListener (NoteListener l ) {
0 commit comments