Java has a huge amount of useful collections and several are made specifically for use in concurrent code like the ConcurrentHashMap. Two sometimes very useful classes are the CopyOnWriteArrayList and CopyOnWriteArraySet. They implement the java.util.List and the java.util.Set interface respectively. Let’s focus on the CopyOnWriteArrayList to understand what it is all about. Contrary to the […]
Java
- Concurrency
- ...
Understanding java.util.concurrent.CompletionService
The java.util.concurrent.CompletionService is a useful interface in the JDK standard libraries but few developers know it. One could live without it as you can of course program this functionality with the other interfaces and classes within java.util.concurrent but it is convenient to have a solution that is already available and less error prone then doing […]
A little functional Java – the map function
Functional programming is getting a lot of interested nowadays. Languages like Clojure or Scala are on the rise and even languages like Haskell which where formerly considered to be mostly academic are getting more and more popular. And more and more Java programmers are thinking about functional programming. Java 8 will get lambda expressions which […]
- Concurrency
- ...
How to use java.util.concurrent.CountDownLatch
With some applications that use several threads there are situations where one thread can only start after some others have completed. For example, image a program that downloads a bunch of web pages, zips them and send then the zip file via email. If you program this in a multithreaded way, the thread that zips […]
BitSets in Scala are much more fun than in Java
Recently I played with BitSets in Java because I needed an efficient way to store huge amounts of long values. For Java there is the java.util.BitSet class. It is a very efficient implementation when you only need to store bit values. Here is a trivial example on how to use it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.BitSet; public class BitSetTest { public static void main(String args[]) { BitSet primeBits = new BitSet(); primeBits.set(2); primeBits.set(3); primeBits.set(5); primeBits.set(7); primeBits.set(11); BitSet evenBits = new BitSet(); evenBits.set(0); evenBits.set(2); evenBits.set(4); evenBits.set(6); evenBits.set(8); evenBits.set(10); //primeBits.and(evenBits); // will result in primeBits only containing 2 primeBits.andNot(evenBits); System.out.println(primeBits); // {3, 5, 7, 11} } } |
It is very […]
Using DirectoryStreams in Java 7
Java 7 comes with lot’s of new stuff for IO. The new interfaces and classes added to the java.nio package contain lot’s of useful functionality for working with files and other things like asynchronous IO. Here I want to show you a little bit about the interface DirectoryStream which is very useful when you want […]
- Concurrency
- ...
How to make Java classes immutable
Immutable classes have been a hot topic lately. The rise of functional languages who operate mostly on immutable data and the advantage of immutable data when using multiple threads (correct immutable classes are thread safe) have also created a new interest in immutable data in Java. While some languages like Scala encourage (but do not […]
File system events with Java 7
In the last post, I showed how to listen to Linux file system events using C, Ruby and Python. In this post, we look at Java 7. Java 7 has several new classes in the java.nio.file package that let you listen to file system events. The number of events available is not as extensive as […]