#Asyncronous future method deadlock - future

I have a method a in class A that calls n times an #Asynchronous method b in class B that calls an #Asynchornous method c in class C.
I want to sync all by Future, but when method b calls methods C.c i have a deadlock.
Do you have any ideas?
Thank you very much
Angeloenter code here
Edit
Class A:
#Singleton
public class StatesPoller {
#Lock(LockType.READ)
private void scheduledTimeout() {
//qmList list with n elements
Collection<Future<QueueManager>> futures = new ArrayList<Future<QueueManager>>();
for (QueueManager qm : qmList) {
if (qm.getQueueManagerStates().get(0)
.getConnectionFails() == 0) {
futures.add(statesPollerUtility.runStateComputation(
qm, df));
}
}
}
Class B
#Singleton
#Lock(LockType.READ)
public class StatesPollerUtility {
#Asynchronous
public Future<QueueManager> runStateComputation(QueueManager qm, SimpleDateFormat df) {
Date startDate = new Date();
QueueManagerState queueManagerState = qmService.getQueueManagerStateByQM(qm);
qmService.setLastPollerStartDate(queueManagerState, qm, new Date());
final Future<Void> queueLocalStateComputation = queueLocalStatesUtility.runQueueLocalStateComputation(qm, df);
queueLocalStateComputation.get();
}
}
class C
#Singleton
#Lock(LockType.READ)
public class QueueLocalStatesUtility {
#Asynchronous
public Future<Void> runQueueLocalStateComputation(QueueManager qm,
SimpleDateFormat df) {
//do something
}
}

What are you returning in every method when you finish, you should return a Future "return new AsyncResult<>" And for what for you need the Locks?. Try to add the returns and get rid of the locks. if a thread 1 lock B, and thread 2 lock C. Thread 2 in order to return the future to B needs that Thread 1 release B, but B only will release B when C will be release by Thread 2, so DEADLOCK.

Related

In Dart, how can I call a superclass method shadowed by a mixin method?

I have a class A and it extends B and mixes in C like the code below. How do I make it work?
class A extends B with C {
#override
int getValue() {
// Call's C's getValue.
super.getValue();
// Now I want to call B's getValue. How do I do that?
return B.getValue(); // Doesn't work.
}
}
class B {
int getValue() {
return 1;
}
}
class C {
int getValue() {
return 2;
}
}
How can I execute B's getValue? Thanks.
The mixin method from C shadows the method from B, so you can't use super directly to refer to the B method. You need to introduce a way to access it.
What you can do is to put a private redirecting function in between the B class and the C mixin application:
class A extends B with _Helper, C {
int getValue() {
// It's C
super.getValue();
// Now I want to get B's getValue how do it?
return super._getValueFromB();
}
}
class B {
int getValue() {
return 1;
}
}
mixin C {
int getValue() {
return 2;
}
}
mixin _Helper on B {
int _getValueFromB() => super.getValue();
}
Since the mixin application order is B, B-with-_Helper, B-with-_Helper-with-C, the super of the B-with-_Helper superclass is B, and the _getValueFromB will access it correctly.

dart await on constructor

What pattern should I use in this example to load and process some data. As value returns a value, it's not acceptable to have d as a Future. How can I get the constructor to wait until load has completed before continuing?
void main() {
var data = new Data(); // load data
print(data.value()); // data.d is still null
}
class Data {
String d;
Data() {
load();
}
Future<void> load() async {
d = await fn(); // some expensive function (e.g. loading a database)
}
String value() {
return d;
}
}
You cannot make a constructor asynchronous.
An asynchronous function needs to return a Future, and a constructor needs to return an instance of the class itself. Unless the class is a future, the constructor cannot be asynchronous (and even then, it's not really the same thing, and you can't use async/await).
So, if your class needs asynchronous set-up, you should provide the user with a static factory method instead of a constructor. I'd usually hide the constructor then.
class Data {
String _d;
Data._();
static Future<Data> create() async {
var data = Data._();
await data._load();
return data;
}
Future<void> _load() async {
_d = await fn();
}
String get value => _d;
}
As an alternative design, I wouldn't even put the load method on the class, just do the operation in the static factory method:
class Data {
String _d;
Data._(this._d);
static Future<Data> create() async => Data._(await fn());
String get value => _d;
}
Obviously other constraints might require that load has access to the object.

Java 8 Streams: List to Map with mapped values

I'm trying to create a Map from a List using Streams.
The key should be the name of the original item,
The value should be some derived data.
After .map() the stream consists of Integers and at the time of .collect() I can't access "foo" from the previous lambda. How do I get the original item in .toMap()?
Can this be done with Streams or do I need .forEach()?
(The code below is only for demonstration, the real code is of course much more complex and I can't make doSomething() a method of Foo).
import java.util.ArrayList;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class StreamTest {
public class Foo {
public String getName() {
return "FOO";
}
public Integer getValue() {
return 42;
}
}
public Integer doSomething(Foo foo) {
return foo.getValue() + 23;
}
public Map<String, Integer> run() {
return new ArrayList<Foo>().stream().map(foo -> doSomething(foo)).collect(Collectors.toMap(foo.getName, Function.identity()));
}
public static void main(String[] args) {
StreamTest streamTest = new StreamTest();
streamTest.run();
}
}
It appears to me it’s not that complicated. Am I missing something?
return Stream.of(new Foo())
.collect(Collectors.toMap(Foo::getName, this::doSomething));
I’m rather much into method references. If you prefer the -> notation, use
return Stream.of(new Foo())
.collect(Collectors.toMap(foo -> foo.getName(), foo -> doSomething(foo)));
Either will break (throw an exception) if there’s more than one Foo with the same name in your stream.

How to get the same instance of a class everywhere it's used inside the object

A little clearer than the question. I want to get the same instance of a class everywhere the class is used inside of an object. I'm using google guice through out the project. Here's an example to better explain.
#Singleton class A {
C c;
D d;
#Inject public A(C c, D d){
this.c = c;
this.d = d;
}
}
class B {
Map<Integer,String> hashmap = new Hashmap<>();
public String getInfo(Integer number){
return hashmap.get(number);
}
}
#Singleton class C {
Class B;
#Inject public C( B b){
this.b = b;
}
}
#Singleton class D {
Class B;
#Inject public D(B b){
this.b = b;
}
}
So each time a new A is created. I want that A's C and D to share the same B , but I don't want all A 's to share one B . Also C and D are being injected into the constructor and A is a Singleton.
You have to define your own scope.
To do that, read the Custom Scopes page on Guice's wiki.

Dependency injection issue (bidirectional communication)

I have a an instance of A and b an instance of B
a must be able to call a method on b and b must then immediatly call a method on a if some checks pass.
To achieve this I would have cyclic DI
public A(B b) { _b = b; }
public void CallToB() { _b.Method(); }
public void Method() { DoSomething(); }
public B(A a) { _a = a; }
public void Method() { if (SomeCheck()) _a.Method(); }
I know I could get arround this, using events and let b be unaware/independant of a. But it would feel wrong.
Note: I haven't seen an answer to this question where bidirectional communication was made possible.
You can solve this issue by depending on interfaces instead of concrete types and then use property injection. Here is an example:
public interface IA
{
void Method();
}
public class A : IA
{
private readonly IB _b;
public A(IB b){_b = b;}
//...
}
public interface IB
{
void Method();
}
public class B : IB
{
private readonly IA _a;
public B(IA a){_a = a;}
//...
}
public class BCycleDependencyBreaker : IB
{
private IB _b;
public IB b
{
set { _b = value; }
}
public void Method()
{
_b.Method();
}
}
You then use BCycleDependencyBreaker when you compose like this:
var b_cycle_dependency_breaker = new BCycleDependencyBreaker();
//Make a depend on this implementation of b that currently does nothing
A a = new A(b_cycle_dependency_breaker);
//Make b depend on a
B b = new B(a);
//Now, let the proxy implementation delegate calls to the real b
b_cycle_dependency_breaker.b = b;

Resources