Skip to content

BehaviorSubject concurrent use in BattleFrontFeed.java #10

@akarnokd

Description

@akarnokd

Hi!

I was watching the video of this code base and saw a common mistake in using Subjects:

private BehaviorSubject<Battle> battleSubject = BehaviorSubject.create();

Subjects by default are not thread safe when calling onNext and lines 34:

.delay(EMIT_RATE_SECONDS / 2, TimeUnit.SECONDS)
.subscribe(battle -> battleSubject.onNext(battle));

and 46:

observable.subscribe(battle -> battleSubject.onNext(battle));

may end up calling it concurrently, which leads to undefined behavior.

Line 26 should look like this instead:

private Subject<Battle> battleSubject = BehaviorSubject.<Battle>create().toSerialized(); 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions