TheCodersCorner.com items tagged with java.

Simple uses of CountDownLatch

By dave

CountDownLatch provides a means of waiting for a number of asynchronous events before proceeding. In order to do this one constructs a latch providing the event count. Then one thread would normally call whilst the other thread calls . Once the count reaches zero the call returns and the latch is set. If the call to happens after...

Formatting dates in Java with DateFormat

By dave

There are several ways to format dates in Java, but by far the easiest is to use DateFormat. Creating a DateFormat is very similar to NumberFormat that we saw on the previous page. Here are the static factory methods called directly on the DateFormat class: getDateInstance(..) getDateTimeInstance(..) getTimeInstance(..) There are several overloaded versions of each method above. We will not cover...

The background, string formatting in java

By dave

Over the years there have been no shortage of ways to format a string in java. What with the + operator, , , and various specialised formatters for numbers and dates we sometimes feel a little spoilt for choice. But how do they all work and what are their advantanges / disadvantages? StringBuffer - a hang up from times gone...

Using Java's XMLStreamReader PULL XML Parser

By dave

In this entry I show how to use the inbuilt Java XMLStreamReader PULL parser class to read an XML file. The XML stream libraries are PULL based XML parsers that do not load the whole document into a memory structure, so therefore are more suited to large volumes of XML. Below is an example XML file for a zoo, it contains...

Using Semaphore and CyclicBarrier in Java applications

By dave

In this example I show an usage of two more concurrent classes and ; which are both provided in the core Java library. There's a wealth of concurrency classes built directly into the JVM that can really simplify multi-threaded development. CyclicBarrrier to make threads wait for alignment. In the example below, I use a cyclic barrier to make several threads...

BlockingQueue producer consumer example

By dave

In this BlockingQueue example we show how to write a very simple producer - consumer with a blocking queue. This example generates SimpleAddition objects that require an addition of two numbers to be performed on the consumer thread. In this case the two values to be added are generated using java.util.Random's nextInt call. They are stored on the queue as a...

Watching files in Java 7 with WatchService

By dave

In this article I show how to watch files and directories for additions/changes/deletions using the new file system support in Java 7. Finally, after years of native solutions, it is now possible to listen for file changes without resorting to OS specific solutions. In the code below, first we use the new file system support class to get a ...

NIO File operations in Java 7

By dave

Hot on the heals of the last article, interested in what other goodies may be in the new file IO package, and wanting to try the new catch block, I cooked up another example. In this example, I create a Path object for an example directory, into which I then create a file and write some text into the file. Some...

Java and Embedded AVR C++ Consulting from TheCodersCorner

By dave

I specialise in multithreaded network programming and embedded C++; with in excess of 20 years experience writing applications in both Java and C++. Having spent many years writing systems that need to communicate using differing protocols, often for exchange connectivity we are familiar with many topologies. In terms of multithreaded development we have built systems with low latency requirements using various...

Using thread local in Java

By dave

This article assumes that you are already familiar with concurrent programming and design. Java 1.5 introduced the concurrent library , which provides an extensive set of classes for dealing with concurrency issues, these sit alongside some existing classes that have been around since earlier times. I've noticed that some of the classes I mention here don't get used as often as...

These may be of interest

We use cookies to analyse traffic and to personalise content. We also embed Twitter and Youtube on some pages, these companies have their own privacy policies.

See the privacy policy and terms of use of this site should you need more information or wish to adjust your settings.