Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 21 - Pages: 2   [ 1 2 | Next ]
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Java and built-in concurrency

At 9:43 AM on Oct 12, 2007, Jim Falgout wrote:

Where is Java going with built-in concurrency? Java5 brought an enhanced memory model and the powerful java.util.concurrent package. And of course Java has always had the Thread class and all of its counterparts. These alone are heads above the capabilities built into C and C++. I'm not familiar with C#, but I assume it is a similar story to Java. However, all of these are building blocks that must be mastered to be put to good use. It could be easier.

Looking at combining the use of annotations and AOP techniques, some additional help can be added to Java in a standard way. For example, the JBoss AOP extensions include a @Oneway annotation. This marks a method to be wrapped at runtime by a thread. This is a very simple concurrency construct as it does not return a value and does not handle exceptions. This article shows how JBoss has used annotations to extend Java for their purposes. Interesting concepts and a good way to use standard Java extensions to add to the language.

Very intersting, but it doesn't solve all concurrency problems. OpenMP for Fortran, C and C++ allows developers to annotate for loops with pragma statements. These pragma statements are used to direct the OpenMP compiler on how to add parallelize the loop. The JOMP (Java OpenMP) effort adds the same constructs to Java. But the JOMP effort seems to have lost steam. The current implementations of annotations and AOP in Java don't seem to help here, as they cannot be applied to functional code blocks such as for loops. This leaves implementing data and nested data parallelism constructs in Java to the developer.

Languages such as Erlang have very easy to use concurrency constructs built in. For example, in Erlang, it is easy to create a "process" that accepts messages and write messages to that process from another. With a few lines of code, sans threading and locking calls, you can write a concurrent program in Erlang. When will such easy concurrency be built into Java? And should it?

I've found some movement in areas mentioned above, but most are experimental and involve either a Java pre-processor or byte code manipulation using prototypes of AOP compilers not widely available. The Java language needs to standardize on this issue or programmers that need concurrency in an easy to use manner will look for other languages to use.
1 . At 10:51 AM on Oct 12, 2007, Jean-Marie Dautelle DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Natural way to deal with concurrency.

Today, there are natural/easy ways to deal with concurrency using vanilla Java. Maybe the simplest solution (unfortunately not mentioned enough) is to use something like Javolution ConcurrentContext . The advantage of this solution is that future Java 7 closure makes it even simpler.
Currently without closure:
ConcurrentContext.enter();
try {
   ConcurrentContext.execute(new Runnable() { 
       public void run() {... }}); // Executed concurrently. 
   ConcurrentContext.execute(new Runnable() { 
       public void run() {... }}); // Executed concurrently. 
} finally {
   ConcurrentContext.exit();
}

Here using Neil Gafter syntax for closure:
ConcurrentContext.enter();
try {
   ConcurrentContext.execute() { ... } // Executed concurrently
   ConcurrentContext.execute() { ... } // Executed concurrently.
} finally {
   ConcurrentContext.exit();
}

And finally using static import:
enter();
try {
   execute() { ... } // Executed concurrently
   execute() { ... } // Executed concurrently.
} finally {
   exit();
}


Could not be easier ;)

It should be noted that ConcurrentContext can be used recursively and are very efficient (e.g. JScience matrix multiplication using concurrent context is 1.99x faster on dual processors!).
Jean-Marie Dautelle - Marlboro, MA
-- Javolution: Everything should be made as simple as possible... -- JScience: But not simpler!
2 . At 12:19 PM on Oct 12, 2007, Mike P wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

But we could build it into the language like so:

{
  concurrent { ... } // Executed concurrently
  concurrent { ... } // Executed concurrently
  concurrent test(); // Executed concurrently.
}


When a scope is exited, it automatically waits for all the concurrent branches to finish.
'concurrent' becomes a keyword, followed by a statement, which can be a single statement, or a set of statements enclosed with the braces.
3 . At 12:25 PM on Oct 12, 2007, Jilles van Gurp DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Java and built-in concurrency

Erlang is a language that a lot of people talk about but that actually few people use. If you try to use it as a drop in replacement for whatever you are using, you will find that it lacks in many respects. For example, Tim Bray has been blogging about his attempts to migrate a simple ruby script to erlang.

The problem is that parallelism is not necessarily something that will be achieved at the low abstraction level of annotating for loops. Java development works differently, you hide everything behind APIs, annotations and component boundaries. Much of the java server infrastructure is already massively scalable so no further changes are needed there to improve scalability.

It's true that though that Java lacks facilities for parallelizing algorithms and that there is a definite increase in demand for that. But then the domains where you actually need to do that are pretty exotic compared to what Java is used for. I don't believe that bolting on a few features to Java is going to address the issue very much. Rather, having support for additional languages and maybe some level of support at the byte code level could be a better way forward. The idea would be to leave parallelizing to the runtime environment and merely to provide clear semantics in the code to aid that process. Obviously this is going to be more easy with languages that are designed to be parallelizeable such as e.g. functional languages.

For people interested in this stuff, there's a series of fascinating lectures on google video on their distributed software and on their map reduce architecture that they use to calculate e.g. pagerank. http://www.samikhan.org/posteditems/2007/10/08/google-mini-lectures-about-map-reduce-and-hadoop Quite interesting stuff.
4 . At 12:50 PM on Oct 12, 2007, mark taylor wrote:
  Click to reply to this thread Reply

Re: Java and built-in concurrency

I think it's a misconception to say that openmp is an optimal solution for writing parallel code. Openmp and it's predecessors MPI and the other one I can never remember the name of have difinite disadvantates. The great thing about Erlang is that it is a 'concurency oriented language' as you noted, features to support concurrency are built into the language. You could even say Erlang is a domain specific language for writing concurrent programs.

Scala is a jvm language that support Erlang style concurrancy; check the Scala website for 'actors'.

But this is all skirting around the issue; concurrency and parallel programming are HARD. There has been a lot of research into auto-parallelizing compilers (HPF-high performance fortran) but we're still far from a production level compiler.

I currently favor Terracota's approach to concurrency. In fact I think a Terracota style framework for c/c++ would be highly useful.
5 . At 2:04 PM on Oct 12, 2007, Jean-Marie Dautelle DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

> But we could build it into the language like ...

True, but then you cannot switch your own implementation (or another implementation). For example, Javolution default implementation allows the user to specify the maximum concurrency (e.g. no more than 50% of CPUs), custom implementations may use particular threads (e.g. NoHeapRealtimeThread) or use particular concurrency policies, etc.
With a library solution you have more flexibility :)
Jean-Marie Dautelle - Marlboro, MA
-- Javolution: Everything should be made as simple as possible... -- JScience: But not simpler!
6 . At 2:13 PM on Oct 12, 2007, Mike Heath wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

In my opinion, it could be even easier than that if you use the Control API syntax as described by Neil Gafter (see http://www.parleys.com/display/PARLEYS/An%20update%20on%20Java%20Closures sections 40-42 for a complete description and good example). With a small modification to ConcurrentContext you could use syntax like:

ConcurrentContext.execute() {
  {...},
  {...},
  {...}
}


In my opinion, that makes the code much more readable because you don't have to explicitly call ConcurrentContext.enter() and ConcurrentContext.exit() and you don't have to worry about the try ... finally block.

What this code is actually doing is invoking ConcurrentContext.execute() and passing in a closure as an argument which is in turn returning an array of closures. It's certainly a different syntax from "normal" Java but once you get used to it really makes sense and adds a lot of flexibility and power to the Java language. Again, see the Gafter closures presentation I referenced above.

I know there are a lot of people who strongly oppose closures but in my opinion, not supporting closures would be leaving Java in the 20th century and I really don't want to see that happen. I think closures are a valuable tool and would make Java a better language for building concurrent applications.
7 . At 2:19 PM on Oct 12, 2007, Jean-Marie Dautelle DeveloperZone Top 100 wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

> With a small modification to ConcurrentContext you could use syntax like:
ConcurrentContext.execute() {
   {...},
   {...}, 
   {...}
}

Wow! This is great (we definitively have to support that) :)
Jean-Marie Dautelle - Marlboro, MA
-- Javolution: Everything should be made as simple as possible... -- JScience: But not simpler!
8 . At 2:21 PM on Oct 12, 2007, Neal M Gafter wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

ConcurrentContext.execute() {
> {...},
> {...},
> {...}
> /code]

I don't think that's syntactically valid. Where's the array creation expression? You can't just tack commas between expressions and pretend the result is an array.
9 . At 2:45 PM on Oct 12, 2007, Jim Falgout wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

That is easy. But it doesn't do much beyond what the @Oneway annotation from JBoss does. How does the work get split up? How can the work output be passed back to the caller (maybe aggregated in some way)? Can the concurrent sections communicate in some way with each other (message passing)?

Maybe the example is simple and I need to go investigate JScience more.
10 . At 2:59 PM on Oct 12, 2007, Jim Falgout wrote:
  Click to reply to this thread Reply

Re: Java and built-in concurrency

I've looked at solutions like TerraCotta and many of the map-reduce solutions by Google. But these are solutions targeted at cluster environments.

With the advent of quad-core processors and massively parallel, single memory systems like Azul, what can I do with Java within that environment? Machines with 8, 16 and on up cores are generally available now for relatively low cost. Many problems can be solved on these machines without the complexity of clustering.

And concurrency is needed, not just for a particular algorithm (the for loop) but also for the whole application. OpenMP and other such frameworks focus on very tight pieces of algorithmic code. But that is usually only one part of the application. Erlang, with its process constructs and message passing allows building pipelined structures that lend themselves to building scalable applications. It seems a combination of approaches is needed.
11 . At 4:09 PM on Oct 12, 2007, Andy DePue wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

Why wouldn't this be covered by Java's new variable arguments?

public void execute(Runnable... processes);
12 . At 4:16 PM on Oct 12, 2007, Neal M Gafter wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

> Why wouldn't this be covered by Java's new variable
> arguments?
>
> public void execute(Runnable...
> processes);


Because these aren't closures and they aren't in argument positions.
13 . At 4:33 PM on Oct 12, 2007, Andy DePue wrote:
  Click to reply to this thread Reply

Re: Natural way to deal with concurrency.

True, but since closures are stil a pipe dream, why not dream of such a syntax? I don't know, maybe if I declare the method like so:

public void execute(SomeClosureType... processes)


The closure syntax would support me doing:

ConcurrentContext.execute() {

{ ... },
{ ... },
{ ... }
}
14 . At 2:05 AM on Oct 13, 2007, Tom wrote:
  Click to reply to this thread Reply

read your literature

There's a literature on parallel languages, parallel language constructs, potential bugs, performance issues, type safety, etc. Instead of engaging in this wild speculation based on exposure to a couple of languages, perhaps people should do some reading first.

As for C/C++, it's not that they have weak parallelism constructs built in, their standards simply don't include parallelism at all. I'm not sure that that's a disadvantage. Java Thread may be more powerful than nothing, but it is questionable whether it is more powerful than POSIX threads, let alone all the other threading and parallelism libraries available for C/C++.

thread.rss_message