« first day (364 days earlier)      last day (3658 days later) » 

9:00 PM
What I said above only holds though if the majority of all the people do not comment when closing.
If the majority would already comment, then of course we should also comment.
 
@skiwi Large sites (SO) I honestly don't care so much about. On Code Review though, the auto comments script I'm using is my best friend (next to monkeys and CRitters of course).
 
On CR I can still understand teaching users would actually give a benefit
 
@SimonAndréForsberg 12000 rep on the 1200th day in beta
I do have a worry though @SimonAndréForsberg about my EventBus
 
Although I've only visited the site 164 of those 1200 days.
Don't worry, be happy, @skiwi.
 
9:05 PM
@rolfl: I'm working on the tag (not too many questions), but I'll stop for now.
 
I am just reading in Effective Java, that (in boldface, which is very sparsely used):
Therefore, it is impossible to exclude concurrent activity from a concurrent collection; locking it will have no effect but to slow the program.
Look what we have here:
@Override
@SuppressWarnings("unchecked")
public <T> void registerListener(final Class<T> eventClass, final Consumer<? extends T> eventListener) {
    Objects.requireNonNull(eventClass);
    Objects.requireNonNull(eventListener);
    if (!classConstraint.isAssignableFrom(eventClass)) {
        return;
    }
    synchronized(eventMapping) {
        eventMapping.putIfAbsent(eventClass, new HashSet<>());
        eventMapping.get(eventClass).add(new ConsumerEventHandler((Consumer<Object>)eventListener));
Does that not directly violate what is said there?
Or perhaps the synchronized should be on this (an EventBus instance) rather than on the eventMapping
 
@skiwi Didn't I say that I think that you don't need to synchronize? I think the ConcurrentHashMap can take care of things for you.
 
@Jamal Well, all that's left now is the .... and there are a number of those.
 
@SimonAndréForsberg I knew on individual operations it would for sure
 
Don't synchronize on this, that can cause problems as you're then leaking the monitor object (this), which allows other parts of your code to synchronize on that. Ask @Mat'sMug for an example of what horrible things (deadlocks) can happen from synchronizing on a public object.
 
9:08 PM
Actually, if my EventBus ultimately only has methods that only use ConcurrentHashMap, is it then not inherently thread-safe?
My bad there.... I would need to use an private final Object lock = new Object() then, which I have done in the past
 
@skiwi I think it is inherently thread-safe then. I'm hoping Monkey can confirm that by a nice little review of your code.
 
@rolfl If there are too many, then you can work on them slowly or no more. I'm just focusing on some of the tags that are missing a small number of questions.
 
@skiwi No, thread safety is not limited to the classes you use. .... and, using Concurrent* does not mean it is safe, in fact, often the worst concurrency problems are masked by incomplete, or incorrect use of java.util.concurrent.
 
Speaking of downvoting/commenting/bad questions:
@nhgrif 5 people of the 11 visitors have watched the video; it would seem you don't speak for the entir internet. Please can you keep ur comments relevant to finding a solution to the question asked. Additionally, your responses so far have not helped. I ask you kindly to stop voting down all my questions. The past two posts i've made you voted down. Why? Why not be supportive of new developers trying to find a solution? Are you above us n00bs now that youve got it all nailed? Your not helping the website by being so negative, so why not join the community spirit & post a solution to my issue? — Dai Lafing 6 mins ago
 
@rolfl That would need more explanation
 
9:13 PM
Just because you use a class that is natively thread-safe, does not mean that your code is safe.
For example, the JDOM code I posted earlier, in the JDOM 1.x version, even though there will never be any corruption in the static data structures, there is in fact a synchronization bug.... and it could be significant.
 
But the only thing my code does, is pass data into a java.util.concurrent class, would it even then not be thread safe?
 
@rolfl What do you mean by "concurrency problems"? Do you mean exceptions being thrown, or performance not as good as it could be?
 
Where's your code? (remind me...).
@SimonAndréForsberg I mean the code (possibly) produces results that are contrary to both the the expectations, and the documentation.
 
Ok, I got the feeling that you wanted to push for as much performance as possible.
 
9:17 PM
@rolfl This was aimed at me and at which code?
 
Uhm. Should any code ever produce results that are contrary to expectations but not documentation or vice versa? Or really... should expectations and documentation ever be at odds?
3
 
When it comes to concurrency, I want to avoid exceptions and have somewhat efficient (and of course, correct) code.
@skiwi I think he wants your question, i.e.
6
Q: My EventBus system

skiwiI decided to roll out my own EventBus system which is intended to be thread-safe. Hence a review should focus extra on thread safety apart from all regular concerns. The EventBus can work in two ways: You can register events and listeners directly on the EventBus. You can the methods, of a sp...

 
*But the only thing my code does, is pass data into a java.util.concurrent class, would it even then not be thread safe?*
^^^^ which code
 
@rolfl ^^^^^^^ that code
 
Ah, so the code I have open in my IDE :p
 
9:20 PM
This code, looks very broken (OK, not very broken, but just broken):
                synchronized (eventMapping) {
                    eventMapping.putIfAbsent(clazz, new HashSet<>());
                    eventMapping.get(clazz).add(new MethodEventHandler(method, callbackObject, clazz));
                }
 
define "broken"
 
So I just learned that synchronizing on eventMapping is utterly useless
 
There is no reason to have a ConcurrentHashMap.
@skiwi In this case, it actually helps, a bit.
 
So what is then broken about it, and what diswarrants the use of concurrenthashmap?
 
Here's the real bug:
@Override
public void executeEvent(final Object event) {
    if (classConstraint.isAssignableFrom(event.getClass())) {
        eventMapping.getOrDefault(event.getClass(), EMPTY_SET).forEach(eventHandler -> eventHandler.invoke(event));
    }
}
^^^ this needs to be synchronized (eventMapping) { ....
> So what is then broken about it, and what diswarrants the use of concurrenthashmap?
 
9:25 PM
@rolfl why does it need to be that?
 
because, in one thread you can be doing:
 
Why would it be...? (Thinking aloud) The classConstraint is a constant after construction, the event is an argument (thread safe), and then it points to a ConcurrentHashMap, is what I think
 
  eventMapping.get(clazz).add(new MethodEventHandler(method, callbackObject, clazz));
and in anther thread you are streaming
eventMapping.getOrDefault(event.getClass(), EMPTY_SET).forEach(eventHandler -> eventHandler.invoke(event));
And, HashSet is not threadsafe.
 
But the HashSet is guarded after ConcurrentHashMap?
 
No it is not.
You are iterating the HashSet after you leave the safety of the Concurrent*
Your HashSet concurrency is guarded by the synchronized (eventMapping) { not by the ConcurrentHashMap.
 
9:28 PM
That is some evil code I breeded there then.
 
Yes.... It is also not using the ConcurrentHashMap properly.
 
But such an excerpt:
synchronized (eventMapping) {
    eventMapping.putIfAbsent(clazz, new HashSet<>());
    eventMapping.get(clazz).add(new MethodEventHandler(method, callbackObject, clazz));
}
Should both eventMapping calls not be synchronized on something private?
On top of using a ConcurrentSet
 
@rolfl What about wrapping HashSet in a Collections.synchronizedSet?
 
The right solution is to not use synchronization on eventMapping
@SimonAndréForsberg No, that does not solve the synchronization problem in the iteration.
(and will likely lead to concurrentmodificationexceptions
The problem with the inside-HashSet is that you have one thread doing slow iteration, and another thread doing short inserts.
You need to ensure that you have exclusive access to the HashSet for the entire iteration.
Synchronizing on the CncurrentHashMap, will ensure that, but, then you lose almost all advantages for having parallel threads... you may as well be single-thread.
Also, synchronizing on a Concurrent* object is just plain ugly, and means that you have a design problem.
Give me a few minutes, I'll propose an alternative.
 
@rolfl You mean that the forEach call is not synchronized? Are you sure that call is not synchronized/thread-safe? Considering it's Java 8, I would assume that they had made it thread-safe.
 
9:38 PM
No, it is not synchronized (and it is potentially parallel, in multiple threads, and, what is/would it synchronized on... the stream? the class, the instance, the what?)
 
Alright, indeed it's not synchronized: docs.oracle.com/javase/8/docs/api
I don't think it's potentially parallel though.
 
btw, You really need to post a more detailed version of the full code as an answer
I'd suppose it would be synchronize on a private lock to the synchronizedset?
The Collections.synchronizedSet mandates that you should synchronize on the site while doing normal iteration (or enhanced for-loop)
However
SynchronizedSet<E> extends SynchronizedCollection<E>, which has:
// Override default methods in Collection
@Override
public void forEach(Consumer<? super E> consumer) {
    synchronized (mutex) {c.forEach(consumer);}
}
Therefore, it is thread-safe?
Even though a stream() is not synchronized
(In SynchronizedCollection<E>)
// Override default methods in Collection
@Override
public void forEach(Consumer<? super E> consumer) {
    synchronized (mutex) {c.forEach(consumer);}
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
    synchronized (mutex) {return c.removeIf(filter);}
}
@Override
public Spliterator<E> spliterator() {
    return c.spliterator(); // Must be manually synched by user!
}
@Override
public Stream<E> stream() {
    return c.stream(); // Must be manually synched by user!
}
@Override
I'd say that Collections.synchronizedSet even smells...
 
How does SynchronizedSet.add look?
 
public boolean add(E e) {
    synchronized (mutex) {return c.add(e);}
}
 
Pretty nice question here (which I've just answered) that was almost pushed off the front page. Anything else to say on this, @Corbin and @JerryCoffin?
 
9:55 PM
0
Q: Is iteration via Collections.synchronizedSet(...).forEach() guaranteed to be thread safe?

skiwiAs we know, iterating over a concurrent collection is not thread safe by default, so one cannot use: Set<E> set = Collections.synchronizedSet(new HashSet<>()); //fill with data for (E e : set) { process(e); } This happens as data may be added during iteration, because there is no exclusive...

And as usual... A simple question gets way out of hand
Time to go now though, wanted to go earlier, but as always interesting things happen ;) goodnight
 
Goodnight!
 
holy, that questions is going to gather upvotes... I'm interested to what I will wake up tomorrow morning
 
People must just be happy to find a decent question on SO. :-)
 
0
A: My EventBus system

rolflThe synchronization in the code is in some places overly broad, and in others, it is absent where it is needed. synchronizing on eventMapping in your registerListenersOfObject method means that only one thread can be accessing the eventMapping instance at any one time. This defeats using the Con...

@skiwi - there. ^^^^^
 
10:18 PM
@Jamal My bit-twiddling skills are quite bad, and you seem to have covered the benchmarking stuff well, so nothing to add from me :0.
 
Again, those things still look obvious to me. I'm not sure of the rest of it myself. I'm sure Jerry will come in with a lengthy answer.
19
Q: If null is evil (and it is) why do modern languages implement it?

mrpyoI'm sure designers of languages like Java or C# knew issues related to existence of null references (see Are null references really a bad thing?). Also implementing an option type isn't really much more complex than null references. Why did they decide to include it anyway? I'm sure lack of null...

Interesting
 
10:53 PM
@skiwi I'm totally impressed by your ability to ask SO questions that gather up-votes.
That said, night all.
 
Goodnight!
 

« first day (364 days earlier)      last day (3658 days later) »