Threading and Javascript

The Javascript console is slowly acquiring features that the other two consoles (ruby and groovy) don't have. The latest addition to this line of features is threading. Here's what we learned from adding threading:

  • We still think threading is tricky. Basically, we kept adding synchronized to places until everything worked. Effective Java says that you shouldn't add synchronized everywhere.

  • The basic problem was this: when executing long commands, the js interpreter grabbed the main thread, and things locked up.

  • We solved this by replacing the js environment with a js thread, which in turn initialized the environment.

  • After that change, commands were executed, but couldn't print things back to the js console.

  • So, we devised a mechanism for sending the js thread a "post-command hook" along with the js command. The post-command hook is a piece of code that the js thread executes after it has executed the js command. Sending code as parameters is exciting!

  • Normally, one puts such code into an anonymous Runnable subclass, but we couldn't do that, since Runnable.run() doesn't take a parameter, and we needed to send a parameter to our hook. So we invented our own class, Hook.

Overall, the Javascript console hasn't really recovered after this set of changes. Some of the additions required excessive scoping, and there are probably refactorings to be made. But overall, our first adventure into the land of threading has paid off: if you run a long, heavy job on the Javascript console, the Bioclipse GUI no longer locks up and stops responding until the job is done.