Skip to contentMethod: getWorkerCount()
1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.processor;
28:
29: import java.util.ArrayList;
30: import java.util.Collection;
31: import java.io.Serializable;
32: import lombok.extern.slf4j.Slf4j;
33:
34: /***********************************************************************************************************************
35: *
36: * @author Fabrizio Giudici
37: *
38: **********************************************************************************************************************/
39: @Slf4j
40: public class LocalImagingTaskProcessor extends ImagingTaskProcessor
41: {
42: private final Collection<PoolThread> workers = new ArrayList<>();
43:
44: /*******************************************************************************************************************
45: *
46: *
47: *
48: ******************************************************************************************************************/
49: protected class PoolThread extends Thread
50: {
51: public PoolThread (final String name)
52: {
53: super(name);
54: setDaemon(true);
55: }
56:
57: @Override
58: public void run()
59: {
60: for (;;) // TODO: add a smart way to terminate
61: {
62: changeFreeWorkerCount(+1);
63: final var task = getNextTask(getName(), false);
64: changeFreeWorkerCount(-1);
65: log.debug(Thread.currentThread().getName() + " assigned to " + task.getName());
66:
67: try
68: {
69: task.prepare(LocalImagingTaskProcessor.this);
70: task.execute();
71: }
72: catch (Throwable t) // prevents task errors to disrupt the worker
73: {
74: log.error("Exception while running task: " + t);
75: log.error("run()", t);
76: }
77:
78: notifyTaskCompleted(task);
79: }
80: }
81: }
82:
83: /*******************************************************************************************************************
84: *
85: *
86: *
87: ******************************************************************************************************************/
88: public LocalImagingTaskProcessor()
89: {
90: for (var i = 0; i < Math.min(Runtime.getRuntime().availableProcessors(), maxWorkers); i++)
91: {
92: final var worker = new PoolThread("PoolThread #" + i);
93: workers.add(worker);
94: worker.start();
95: }
96:
97: log.info("Created " + workers.size() + " local workers");
98: }
99:
100: /*******************************************************************************************************************
101: *
102: * {@inheritDoc}
103: *
104: ******************************************************************************************************************/
105: @Override
106: public boolean isDistributed()
107: {
108: return false;
109: }
110:
111: /*******************************************************************************************************************
112: *
113: * {@inheritDoc}
114: *
115: ******************************************************************************************************************/
116: @Override
117: public boolean hasFileAccess()
118: {
119: return true;
120: }
121:
122: /*******************************************************************************************************************
123: *
124: *
125: ******************************************************************************************************************/
126: @Override
127: public int getWorkerCount()
128: {
129: return workers.size();
130: }
131:
132: /*******************************************************************************************************************
133: *
134: *
135: ******************************************************************************************************************/
136: @Override
137: public Collection<Serializable> getWorkerIds()
138: {
139: final Collection<Serializable> result = new ArrayList<>();
140:
141: for (final var worker : workers)
142: {
143: result.add(worker.getName());
144: }
145:
146: return result;
147: }
148: }