Task Queue for Heavy Weight Tasks in JavaSE Applications

Modern hardware systems have a multi-core architecture. So in contemporary software development concurrency is an even more crucial ingredient than before. But as we will see it is of great importance for single core systems, too. If you have already created a Java Swing application you’ve propably made an acquaintance with the SwingWorker, in order to delegate long running tasks from within Swing events to another thread. But first things first. In Swing the whole painting and event handling of the graphical user interface is executed in one thread the so called event dispatching-thread from AWT the underlying former window toolkit from Java. Therefore most of Swing-based methods are not thread-safe, meaning that you have to prevent race conditions by yourself, but it offers the possibility to dispatch tasks to the event-dispatcher thread. It is highly recommended to do everything, that updates the GUI in the corresponding thread.

This works as follows. The event dispatcher maintains a queue in which all UI-Events and external runnables, submitted via SwingUtilities#invokeLater() or SwingUtilities#invokeAndWait(), are enqueued and executes them one by one:
The Event Dispatching Queue

If you have a long-running task, like searching a file on the hard drive, being invoked by an event, e.g., a button click, you should not do this in the event-handling code, since you would block the whole queue until the task is finished leading to an unresponsive GUI experience. This is also true for single-core machines. Lets take a look at this. Imagine you have a machine with one core and you are executing a task like mentioned before. If you are doing this in the queue, you won’t be able to cancel the task or to do anything on the GUI. It will freeze. But good GUIs don’t freeze, even on single-core machines. Why is that? If you dispatch this task to another thread and w.l.o.g it iterates through a big loop, then you may call Thread#yield() in every iteration, in order to give the Java VM a hint that it can suspend this thread and give some calculation capacity to the GUI thread. In addition if it waits for example for an I/O-operation, the GUI thread can process some GUI events. Since Java is preemptive (on most machine/operating system combinations), it can (even without ‘yield’-hints) interrupt the external thread at any time, in order to give the dispatcher thread some time and vice versa. So this is (on single-core machines) not real concurrency, but it allows both threads to work alternatingly, which appears to be concurrent. This especially applies for multi-core machines. Furthermore the external task can check for Thread#isInterrupted() or if it waits, e.g., for interruptable I/O operations it can react to cancel-events of the user and terminate the execution prematurely. Here is a code example:

while(!finished) {
  if(Thread.interrupted()) {
    cleanup();
    return;
  }
  doStuff();
  Thread.yield();
}

It is of fundamental significance, that you program long-running tasks this way. If you counteract, you provoke tasks that cannot be canceled, leading to annoying system behavior. The tenet reads: The longer the task, the more eager it should be to react to cancellation.

Analogous to dispatching external execution to the swing thread by enqueuing it, long-running tasks should be send to external threads. For this purpose the Swing Worker has been developed and since Java 5 it is an integral part of the JRE and evolved in Java 6. With the SwingWorker you can execute a task in another thread and relieve the event dispatcher. In the early versions of the Swing Worker it started new threads for every execution and had no support for getting intermediate progress from it conveniently and show it, e.g., in a progress bar. So I implemented a second queue for executing long-running tasks on an external thread for Java 1.4.2. Lateron I refined it by using the ExecutorService of Java 5. Now in Java 6, the Swing Worker uses an ExecutorService by itself with a fixed number of worker threads and offers very convenient features for updating the GUI with progress and canceling an execution.

Was it worth implementing my own heavy weight task executor? Yes! Since the SwingWorker starts a fixed number of worker threads, that cannot be configured there might arise race conditions between the data structures and resources accessed in the external tasks. If you are a fan of the one-thread-philosophy of Swing, because it naturally serializes/sequentializes the access to the resources you may use a second queue for these tasks and exchange tasks between the GUI thread and the external thread. In this sense, long-running tasks are sent to the heavy weight task executor and GUI updates — like progress updates — are sent back.

How this can be achieved will be the topic of another post (as usual with a running code example for a rudimentary heavy weight task executor).

Posted in Java, Knowledge. Tags: , , . No Comments »

Exception Handling for Injection Interceptor

Remember my post “Circular Injection of Util Classes in EJB 3.0“? There I offered a some kind of ugly solution to the circular dependency problem for managed classes in Java EE 5. In a preceding post (Circular Dependencies of Session Beans) I grumbled about the exception handling in jBoss-5.1. It lets you alone with a meaningless error message and you have to guess what the problem is. Unfortunately my own code presented in the former post is even worse, since it logs problems but ignores them. It wasn’t mentioned for productive use, but it was annoying to me, so here is a little tune up adding exception handling and readable error messages.
(more…)

Circumvent Nested Transaction Issues in Tapestry-5.x

Ajax component events may be wrapped in a transaction as I pointed out in “Transaction Handling for Ajax Components in Tapestry-5.x“. But on some occasions an Ajax component event is surrounded by a component event. So the code in the ControllerUtil of article “Transaction Handling in Tapestry5” will lead to ‘transaction already active’ problems, since we try to begin a transaction in the nested ajax component event although there is already an active transaction attached to the current thread. We can overcome this situation by checking, whether an active transaction is present and begin/commit/rollback a new transaction iff not. This behavior is similar to the default transaction attribute REQUIRED in Java EE.
(more…)

Concurrent conversion of SVG to formats like PNG, EPS or PDF

In this post I’ll show how to use the ExecutorService of the java.concurrent package, in order to start as many inkscape shells as processors available on the current machine and to distribute a whole bunch of conversion tasks wisely on the cores. On my quad core I got a speedup of about 3.5, which is really near to 4.
(more…)

Informative Exceptions with Java Proxies

Information is essential for reasonable exception handling. Unfortunately, it is not always affordable to write custom exceptions providing all necessary information, especially in test code, e.g. for selenium tests. So, here is an approach to weave informative exceptions into your [test] code.
(more…)

Circular Dependencies of Session Beans via Manual EJB Lookup

In my last two posts (Circular Dependencies of Session Beans and Circular injection of Util Classes in EJB 3.0) I wrote, that it is possible to have circular dependencies between session beans via interceptors and manual EJB lookup. In this post I will sketch out how.
(more…)

Circular Injection of Util Classes in EJB 3.0

In my last post Circular Dependencies of Session Beans I presented a method to use interceptors in session beans in order to inject beans. This works great until you want to add circular dependencies. Then you have to look up the beans by name and inject them into the bean. But this is kind of cumbersome. So, if it is possible, have a bean structure, which is topologically sortable and inject util classes having circular dependencies between each other. This post shows how to achieve the latter.
(more…)

Circular Dependencies of Session Beans

This post is about circular dependencies between session beans (ejb 3.0), which is ‘not possible’ without manual loading. The manual variant might be the solution of choice for you. Therefore, I will sketch it out later in this post. The first part of this post post is about a trial to achieve this with @EJB annotation only … which failed! But perhaps it will stop some of you to try it out (for nuts) and it’s a great bridge to a solution by a snatch enabling to have circular injection of ‘your own’ beans. I will show you the latter in my next post.
(more…)

Posted in Java, Knowledge. Tags: , , . No Comments »

Menus with Icons in Ubuntu Karmic++

With the update to karmic koala ubuntu reversed the color scheme of context menus. Before the foreground had been dark and the background bright. Now, guess what, it’s the other way around. With this change, the property for showing icons in context menus has been disabled, too. This is due to transparency problems. Unfortunately, programs like eclipse make extensive use of this feature. So, not having these icons anymore is more than annoying.
(more…)

Standalone Tomcat with jBoss (2nd Edition)

This tutorial desribes, how to install and configure a standalone Tomcat, so that a deployed webapp can connect to a jBoss and use the authentication of the application server. This method is decoupled from the login module or authentication type (LDAP, Database, …), respectively. It differs from the approach described in Standalone Tomcat with jBoss plus authentication against LDAP, in that it allows for parallel logged in users and it does not need to authenticate to LDAP/Database on both sides, but on the jBoss only.

(more…)

Switch to our mobile site