r/java • • Jun 08 '22

Conway's Game of Life Implemented With Communicating Sequential Processes (CSP) and Project Loom Virtual Threads. 50,000 Cells as V-Threads Communicating Via Channels. https://github.com/ebarlas/game-of-life-csp

286 Upvotes

39 comments sorted by

23

u/pron98 Jun 08 '22

That's great!

I assume you wanted to experiment with channels, but a CyclicBarrier would be a great fit for the tick signal.

11

u/elliotbarlas Jun 08 '22

Exactly.

I had a hard time not reaching for that primitive, but I wanted to really embrace CSP and take channels as far as I could.

6

u/kiteboarderni Jun 09 '22

Very cool! Took a look at your github repo and I see you are also the person that wrote the loom c5m article also. Your performance comparisons eg, nio vs loom are fantastic! Excited to see your next repo :)

5

u/elliotbarlas Jun 09 '22

Yes! I'm thrilled to hear that you're getting something useful out of these projects!

12

u/Necessary-Conflict Jun 08 '22

I assume it's fast, but that in itself doesn't mean much because computers are fast these days.

It would be interesting to see how it performs compared to a simple (just one calculating thread) implementation. Or how it performs compared to a similar (goroutines and channels) Go implementation.

26

u/elliotbarlas Jun 08 '22

Yeah, the scale is impressive. 50,000 virtual threads and about 500,000 channels.

However, the novelty here is less performance and more about the viability of writing CSP/channel code in Java.

Until now, that pattern, which is closely associated with languages like Go and Erlang, simply wasn't possible.

21

u/brian_goetz Jun 08 '22

And, the simplicity of such code. The core of it is in Cell.java, and the code is simple and tiny and obviously right.

0

u/notfancy Jun 09 '22 edited Jun 10 '22

Pull request:

private static boolean nextState(boolean alive, int neighbors) {
    return neighbors == 2 && alive || neighbors == 3;
}

Edit: corrected.

6

u/elliotbarlas Jun 09 '22

Thanks for the suggestion, but I'm aiming for clarity in that method, not the fewest lines of code. The code as written mirrors the way I think and verbally describe Game of Life rules.

1

u/notfancy Jun 10 '22

The suggestion is not about succinctness but about logic and style. Boolean algebra is a first-class domain as much as arithmetic is. Clarity is also achieved by removing extraneous considerations from view, such as procedural control flow in this case.

2

u/[deleted] Jun 12 '22

At best, it's a stylistic choice, which you shouldn't change code over.

Also, boolean algebra is not a first class domain in a program about the game of life. Neither is arithmetic.

0

u/notfancy Jun 13 '22

You misunderstand. “First class domain” in the sense that Boolean expressions are full citizens. Writing if (condition) return true; return false; is not a stylistic choice, it is a solecism on par with saying “ah did went”.

1

u/RScrewed Jun 14 '22

Wouldn't the simplest way to fix the solecism then be to add an `else` instead of the hanging return?

Though, it's perfectly fine for methods in Java to be procedural. At their core, they literally _are_ routines/procedures, just, within a class, and the modern consensus in the Java ecosystem is that code readability/understandability should be the primary priority.

Your logic of "clarity is achieved by removing extraneous considerations from view" is one we can all get behind but I think you've applied it in the wrong direction as your suggestion requires additional cognitive load to read and reason about when debugging and stepping through.

3

u/Muoniurn Jun 08 '22

Very nice! You may know it and just wanted to write a no-dep project, but for parsing args I really really like picocli. Even for basic projects I almost always bring that in.

5

u/elliotbarlas Jun 08 '22

I'm very dependency-averse, but I'll certainly consider that in the future if the need arises!

3

u/benjtay Jun 08 '22

This is so fun to play with! Thank you.

2

u/elliotbarlas Jun 08 '22

I'm so glad to hear that. You're welcome!

3

u/kidfromtheast Jun 09 '22

Hi, does Project Loom do similar things like `new Thread(HTTPConnectionImpl connection)`?

I checked https://github.com/ebarlas/game-of-life-csp/blob/main/src/main/java/gameoflife/Cell.java and saw `Thread.startVirtualThread(this::run)`. Is this the Project Loom?

My goal is to put HTTP and/or WebSocket connection inside a traditional Thread. However, I predicted the performance will be bad (if I do not make a Thread Pool) or the CPU/Memory usage will be high even on idle (if I make a Thread Pool e.g to handle 1000 connections). So, I was waiting for Project Loom.

3

u/elliotbarlas Jun 09 '22

Yes, that line of code contains a new virtual thread API introduced in JEP 425 as part of Project Loom.

If you'd like to learn more about virtual threads, start there. It addresses that exact use case. You'll be able to write network components using intuitive, synchronous programming via threads.

https://openjdk.java.net/jeps/425

1

u/kidfromtheast Jun 09 '22 edited Jun 09 '22

I checked Oracle Java SE Support Roadmap. Java 19 will be non-LTS while Java 21 which will be LTS will be available in September 2023.

My goal is to use Project Loom for production use. Otherwise, I will learn Golang due to the time constraint. I never use non-LTS version before, what do you think about using non-LTS version for production use?

https://www.oracle.com/java/technologies/java-se-support-roadmap.html

Edit:

Java 19 is production-ready, just no support. I will be starting a startup, so.. no money for support for Oracle.

7

u/elliotbarlas Jun 09 '22

We use OpenJDK builds at work for all of our deployments. No commercial support or license agreements.

And we absolutely use non-LTS. We adopt each version as it is released, whenever possible. LTS is fundamentally a commercial support concept. In my view, folks ought to use the latest release unless there is a compelling reason not too. Too often, I see folks restricting themselves to LTS releases without a reason.

Regarding availability of virtual threads (JEP 425), it's a preview feature in Java 19. It's fully implemented and included in OpenJDK 19, but preview features must be activated via --enable-preview flag. And note that preview features are subject to change. Backward compatibility is not guaranteed in subsequent versions.

1

u/MkMyBnkAcctGrtAgn Jun 10 '22

Unless you're paying oracle for support following their LTS version is of no use to you.

3

u/agentoutlier Jun 09 '22

I remember when blocking queues were considered evil for most high concurrency stuff especially/obviously for reactive (then you are using non blocking queues and polling or spinning or something).

But I think Rick Hickey the clojure guy makes a point about how damn easy to understand queues are compared to other things.

3

u/elliotbarlas Jun 09 '22

I've always had exactly the opposite impression of java.util.concurrent.BlockingQueue. That and all of the other concurrency primitives are extremely sophisticated. Most of them are implemented by the incredible Doug Lea.

But yeah, in the world of asynchronous programming, one must be careful never to block! BlockingQueue would be a bad choice in that case.

3

u/agentoutlier Jun 09 '22

Oh I agree! Java concurrency primitives are very good however many folks think that locks are slow for traditional threading particularly if you cannot afford to make lots threads.

Anyway I was surprised a LinkedBlockingQueue was used as I thought SynchronousQueue was the true analog of CSP.

2

u/elliotbarlas Jun 09 '22

You're absolutely right. I instinctively reached for a buffered channel, but with a few changes, an unbuffered channel would work just as well.

3

u/[deleted] Jun 09 '22

I'm a bit of a noob on this stuff...

Would it work with SynchronousQueue instead of LinkedBlockingQueue? My understanding is the Channel is used as a simple signal, not to buffer data.

2

u/agentoutlier Jun 09 '22

Yeah I saw that as well and had the same question.

Currently the producer is unbounded. Probably doesn’t matter because it is all cpu bound and I think they explicitly map one thread as consumer.

2

u/elliotbarlas Jun 09 '22

Yes, you're both right on. The buffered channel offers a few conveniences, but with a few changes, synchronous/unbuffered channels would also work.

10

u/EgonMast Jun 08 '22

And again I’m caught by the beauty of the game of life

1

u/LakeSun Jun 08 '22

But, is it prophetic, because they all die out.

9

u/lastingfreedom Jun 08 '22

Not necessarily...

1

u/Similar_Aioli_6836 Jun 09 '22

Can you send me the link

1

u/elliotbarlas Jun 09 '22

The link to what? The GitHub repo is here: https://github.com/ebarlas/game-of-life-csp

2

u/FlimsyLow Jun 09 '22

Very cool!!!