I am always experimenting with new technologies just to see what new cool things are possible. As a Java developer, Iâ€™m usually toying with JVM-related technologies. The latest new and cool technology that I have been experimenting with is TeaVM: A JVM for the browser. After using it for about 2 months now, I am confident in saying that TeaVM is awesome!
TeaVM vs GWT
- TeaVM supports (or can potentially support) alternative JVM languages like Scala, JRuby, Kotlin, etc, since it operates on .class files (which all JVM languages ultimately compile to). GWT is limited to just Java.
- Multithreading. This is groundbreaking. TeaVM actually supports threading primitives like synchronized, Thread.sleep(), Object.wait/notify, etc.. This is a very new feature of TeaVM. Many have tried to come up with a threading solution in the browser, but TeaVM is the first (in history?) to have successfully done it. See live demo of multithreading
What Iâ€™m Using it For
A little over a year ago I came across TeaVM for the first time, and I asked Alexey (its author) whether there was any support for threading. He said â€œnoâ€ at the time, but that he had an idea of how he could implement green threads. At that time I wasnâ€™t working for Codename One and had limited time to spend on projects like this, so I just left it there.
Aside: I was a little naive at this point to think I could do the port with my current knowledge and tools. Knowing what I know now, there is no way I could have succeeded without the the help of Alexey and TeaVM.
At this point I thought I could take the code that was generated by TeaVM and modify it to use Stratifiedjs to support continuations/pseudo threads. He replied that he was aware of this project but that it wasnâ€™t a good fit because he had a better way.
The full thread that shows the development of Async code generation (i.e. multithreaded support) can be see here.
Within days, he had completed a prototype of his new â€œCPS styleâ€ code generator that supported asynchronous code. He implemented Thread.sleep() and Thread.yield() and posted a test case. Based on these implementations, I added initial support for Object.wait(), Object.notify() and a few other threading primitives. Alexey, has since refactored and improved these implementations, such that probably none of my original contributions are recognizable. The full scope and scale of this project, frankly were a little over my head.
The Poker Demo
Here is a small teaser of the result of the new Codename One port that is built using TeaVM: the Poker demo. The application itself was not modified from the version deployed to iOS, Android, etc… It just required a port layer to implement the “native” components of the Codename One framework, and the rest was left up to TeaVM to work its magic.
Another nice thing about TeaVM is that it pays attention to things like executable size. It uses static analysis to strip out dead code prior to conversion so that your app is as small as possible. It also includes an option to minify the code, which further reduces the code size.
More to Come
Personally I think that Java needs to be in the browser, since it is essentially the â€œoperating system of the futureâ€. I also think that the browser can benefit greatly from Java as applications get more complex and difficult to maintain. Currently TeaVM provides the best route between Java and the browser, so I expect it to catch on over the following months and years.
Do you currently have a project that depends on GWT? Or do you have a Java project that you would like to offer on the web? You may want to consider porting it to TeaVM because of all of its nice features (i.e. support for other JVM languages, and multithreading). I have had a great time porting Codename One to TeaVM. Iâ€™m happy to answer questions or offer tips if you are interested in porting your project over as well. In addition, I think youâ€™ll find that Alexey is very responsive to bug reports, feedback and questions.
For more information about TeaVM, check out its Github repository.