In this example I am going to demonstrate how to write a java applet with a background thread.
Here is a list of the use cases for code written as such:
- Java Applet
- Java Console Application
- .NET Console
Additionally I have added support for mouse enter and leave events the java way. While in .net it is easy to subscribe to events using anonymous delegates, such a feature is not yet supported by jsc compiler for java target. It could be resolved in the future releases by adding support for generics and build dynamic invokation on reflection. Google App Engine and Android markets are probably going to trigger that update.
For what do we need a background thread
I have created a simple infinite incrementing loop to serve as our business layer. There is a special class ThreadedAction, which abstracts away thread creation, which in turn differs from .net to java from being delegate based to being interface based.
24 while (true)
25 {
26 Value++;
27
28 // we should be running on our own thread
29 // which enables us to loop forever and sleep when tired
30 Thread.Sleep(100);
31 }
How do we spawn a new thread
As I enjoy the AtDelay and AtInterval extension methods I make use of while programming for actionscript and javascript, I had to implement something similar for the threading API. Therefor spawning a new thread is dead simple:
57 CurrentThreadedAction = 0.AtDelay(Current);
Why not try to test it out running as a console.
The assembly has been defined as a console exe. Hitting F5 in debug build will show you the console asking your input when to stop the computation.
We now saw that our threaded application does run inside .net. It is time to run it on java virtual machine. To do that we need to build our project with release build. This will trigger jsc to genereta java source code and afterwards java compiler will add final magic.
You cannot just press F5 now, instead you should start run.jar.bat. I am using Scite and by double clicking on the bat file Scite shows up in which I can press F5.
Notice that, only the title is different.
Let’s add an applet interface
Console applications are cool, but applets also have its role to play in our example. The applet form is set up in two steps. In file DemoApplet.Designer.cs I am going to define the buttons and attach handlers.
37 this.Button3 = new Button {};
38 this.Button3.setLabel(“Compute”);
39 this.Button3.addMouseListener(new Button3_MouseEnter_Handler { Target = this });
40 this.Button3.addMouseListener(new Button3_MouseExit_Handler { Target = this });
41
42 base.add(Button3);
I know you would rather use delegates – I would, but I cannot just yet. In file DemoApplet.cs I am going to define the actual handlers.
97 #region [this.Button3_MouseEnter]
98 [Script]
99 class Button3_MouseEnter_Handler : MouseListener_MouseEnter
100 {
101 public ThreadingExample Target;
102
103 protected override void Invoke()
104 {
105 Target.Button3_MouseEnter();
106 }
107 }
108 #endregion
109
110 public void Button3_MouseEnter()
111 {
112 this.Button1.Enabled = false;
113 this.Button2.Enabled = false;
114
115 this.MyComputation.Start();
116
117 this.Button1.setLabel(“start @ “ + MyComputation.Current.Value);
118 this.Button2.setLabel(“Stop computing”);
119
120 }
In the applet I could start the computation either by explicitly clicking on the start button or hovering your mouse on the third button.
Summary
In this example I demonstrated how to write a threaded applet which handles mouse enter and mouse leave events for starting and stopping computation.
| ThreadingExample |
![]() |



















