Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

980 fix unstopped threads #293

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ public <R extends ObjectReceiver<T>> R setReceiver(final R receiver) {

@Override
public void resetStream() {
queue.add(Feeder.BLUE_PILL);
queue.put(Feeder.BLUE_PILL);
}

@Override
public void closeStream() {
queue.add(Feeder.RED_PILL);
queue.put(Feeder.RED_PILL);
try {
thread.join();
} catch (InterruptedException e) {
Expand Down Expand Up @@ -147,6 +147,7 @@ public void run() {
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* Copyright 2019 hbz, Pascal Christoph.
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.metafacture.flowcontrol;

import org.metafacture.flowcontrol.ObjectPipeDecoupler;
import org.metafacture.framework.ObjectPipe;
import org.metafacture.framework.ObjectReceiver;
import org.metafacture.framework.Tee;
import org.metafacture.framework.annotations.In;
import org.metafacture.framework.annotations.Out;
import org.metafacture.framework.helpers.DefaultTee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Divides incoming objects and distributes them to added receivers. These
* receivers are coupled with an
* {@link org.metafacture.flowcontrol.ObjectPipeDecoupler}, so each added
* receiver runs in its own thread.
*
* @param <T> Object type
*
* @author Pascal Christoph(dr0i)
*
*/
@In(Object.class)
@Out(Object.class)
public class ObjectThreader<T> extends DefaultTee<ObjectReceiver<T>> implements ObjectPipe<T, ObjectReceiver<T>> {

private static final Logger LOG = LoggerFactory.getLogger(ObjectThreader.class);
private int objectNumber = 0;

@Override
public void process(final T obj) {
getReceivers().get(objectNumber).process(obj);
if (objectNumber == getReceivers().size() - 1)
objectNumber = 0;
else
objectNumber++;
}

@Override
public <R extends ObjectReceiver<T>> R setReceiver(final R receiver) {
return super.setReceiver(new ObjectPipeDecoupler<T>().setReceiver(receiver));
}

@Override
public Tee<ObjectReceiver<T>> addReceiver(final ObjectReceiver<T> receiver) {
LOG.info("Adding thread " + (getReceivers().size() + 1));
ObjectPipeDecoupler<T> opd = new ObjectPipeDecoupler<>();
opd.setReceiver(receiver);
return super.addReceiver(opd);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class DefaultTee<T extends Receiver> implements Tee<T> {
private final List<T> receivers = new ArrayList<T>();

@Override
public final <R extends T> R setReceiver(final R receiver) {
public <R extends T> R setReceiver(final R receiver) {
receivers.clear();
receivers.add(receiver);
onChangeReceivers();
Expand All @@ -52,7 +52,7 @@ public final <R extends T> R setReceivers(final R receiver, final T lateralRecei
}

@Override
public final Tee<T> addReceiver(final T receiver) {
public Tee<T> addReceiver(final T receiver) {
receivers.add(receiver);
onChangeReceivers();
return this;
Expand Down