Multithreading Applets—the Right Way

Multithreading Applets—the Right Way

The canonical design of multithreaded applets is inherently flawed. You know what I’m talking about: the run method with the repaint/sleep statements embedded in a while(true). Any self-respecting operating systems instructor would fail that design with reckless abandon. Here’s the offensive code:

 public void run() {        while (true) {          repaint();          try {           Thread.sleep(100);          } catch (InterruptedException e) {}        }      } 

Of course, we also have a paint method somewhere, which does all our impressive applet drawing. What’s wrong with this model? The problem is in the sleep statement. Simply speaking, there is no perfect number to put in the sleep statement. The sleep statement is there to hold off calling repaint until we know the VM inside our browser (or appletviewer) completed a call to our paint method. Unfortunately, you can never anticipate how long that will take. That is dependent on the machine, OS, length of the paint method, and what else happens to be going on in the OS at the same time. Even if you found a number that seemed to work well, it may well fail if the OS gets busy with other transient functions, and it may be disastrous when moved to a less powerful machine.

The bottom line is that you are trying to synchronize two threads with that sleep statement. The two threads are your “run” thread and the thread the browser sends in to call your paint. That is synchronization on a “guess” (Dr. Tanenbaum, please forgive us). This is seriously wrong.

How can we solve this problem correctly? In other words, how can I guarantee that only one repaint is called for exactly one paint call? Try this:

 public synchronized void run() {        while (true) {          repaint();          try {           wait();           Thread.sleep(100); // optional          } catch (InterruptedException e) {}        }      }public void paint(Graphics g) {        // drawing        synchronized(this) {          notifyAll();        }    }

What have we done? If you follow the flow you’ll see that we issue a repaint and then wait. The run thread cannot continue until the paint method is done drawing and as its last act, notifies waiting threads. This paradigm guarantees one repaint per paint. We can still put in a sleep to control how much CPU a possibly overzealous applet may take, but it is by no means synchronizing the threads.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes