Kyronet and Libgdx running in same thread

171 views
Skip to first unread message

Tomaž Kunaver

unread,
Jan 2, 2017, 5:42:10 AM1/2/17
to kryonet-users
Hi,
I've been using Kryonet with my Libgdx app so far nad ran into problems: when I update some UI controls from kryonet's client listener, app crashes occasionally. This is due to thread concurrency problems. Now I was thinking how to synchronize main libgdx thread and kryonet thread. My question is: what is the best practice, how do you guys do it?

I've tried doing it like this:

Gdx.app.postRunnable(new Runnable() {
@Override
public void run() {
client.update();
}
});

This ensures that the kryonet's client update gets executed within the libgdx's thread. However, there is one problem: connect() method must be ran in a separate thread, or else this happens:
Exception in thread "LWJGL Application" java.lang.IllegalStateException: Cannot connect on the connection's update thread.

Now my problem is that the connect() method calls onConnect notifier method which in turn adjust UI controls in my app. This again causes concurrency problem. So my question is: how to resolve this problem?

Joachim Durchholz

unread,
Jan 2, 2017, 6:17:35 AM1/2/17
to kryone...@googlegroups.com
Am 02.01.2017 um 11:42 schrieb Tomaž Kunaver:
> Now my problem is that the connect() method calls onConnect notifier
> method which in turn adjust UI controls in my app. This again causes
> concurrency problem. So my question is: how to resolve this problem?

Make the adjustment method use postRunnable as well.
If you find this to be a huge can of worms, then the design decisions in
your application may need to be revisited. Advice for this can be found
by searching for
"best practices" multithreading

(Sorry for being off-topic from Kryonet.)

Tomaž Kunaver

unread,
Jan 2, 2017, 9:12:36 AM1/2/17
to kryonet-users
Nevermind, I solved the issues. I introduced ConcurrentLinkedQueue and I add all events into that (received, connected, disconnected). However, the whole solution is somewhat complex. Would be nice if this was already implemented in Kryonet in some way so that we wouldn't do have to do all these stuff in order for it to work.

Joachim Durchholz

unread,
Jan 2, 2017, 10:23:48 AM1/2/17
to kryone...@googlegroups.com
Am 02.01.2017 um 15:12 schrieb Tomaž Kunaver:
> Would be nice if this was
> already implemented in Kryonet in some way so that we wouldn't do have
> to do all these stuff in order for it to work.

That's not a simple thing to do - what's good enough for your
application isn't necessarily useful for everybody else.
Reply all
Reply to author
Forward
0 new messages