Home » Core Java » util » Page 30

util

Reentrant Lock example of a task runner

With this example we are going to demonstrate how to implement a ReentrantLock of a task runner. The ReentrantLock is a reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities. The custom Class that have implemented is called TaskRunner and consists of the ...

Read More »

Reentrant ReadWriteLock example of value calculator

This is an example of how to use a ReentrantReadWriteLock of a value calculator. We have implemented a method that uses a ReadWriteLock and implements the calculate(int value), the getCalculatedValue() and the getValue() methods. In short the class is described below: It creates a ReentrantReadWriteLock. First it calls calculate(int value) with the int value that is to be calculated. The ...

Read More »

Semaphores example limiting URL connections

In this example we shall show you how to use a Semaphore for limiting URL connections. We have implemented a class, ConnectionLimiter that uses a Semaphore and is described below: It creates a new Semaphore with a given number of permits. It implements a method, URLConnection acquire(URL url). The method acquires a permit from this semaphore, blocking until one is ...

Read More »

Exchanger example passing logs to a background logger

With this example we are going to demonstrate how to create an Exchanger passing logs to a background logger. We have implemented a class, BackgroundLogger that implements the Runnable interface. In short the class is described below: It consists of a static class LogEntry, that has three fields, a long time, an int value and a Stringbuilder, and a static class LogEntries, ...

Read More »

Store preferences to user space

This is an example of how to store Preferences to user space. The Preferences class allows applications to store and retrieve user and system preference and configuration data. Storing Preferences to user space implies that you should: Get the root preference node for the calling user, using userRoot() API method of Preferences. Get the named preference for the given pathname of the ...

Read More »

Read preferences from user space

In this example we shall show you how to read the Preferences from user space. To read the Preferences from user space one should perform the following steps: Get the root preference node for the calling user, using userRoot() API method of Preferences. Get the named preference for a given path name of the preference node, with node(String pathName) API ...

Read More »

Simple validation example

In this example we shall show you how to make a simple validation of a String, using a Matcher against a specified Pattern. To make a simple String validation one should perform the following steps: Create a new Pattern, by compiling to it a regular expression. The regular expression constructed here is the word “Java” followed by a space character ...

Read More »

Find duplication in strings example

With this example we are going to demonstrate how to find duplication in Strings. In order to check if there are duplications in a String we have created a Matcher against a specified Pattern and used the Matcher API in boolean hasDuplicateWord(String phrase) method. In short the steps of the method are: Compile a String regular expression to a Pattern, ...

Read More »

Determine if a string matches a pattern exactly

This is an example of how to determine if a String matches to a Pattern exactly. Using a Matcher to check if a String matches to a specified Pattern implies that you should: Compile a given String regular expression to a Pattern, using compile(string regex) API method of Pattern. The given regex in the example is the character “b”. Use ...

Read More »

Matcher.appendReplacement example – Part 2

In this example we shall show you how to use Matcher.appendReplacement(StringBuffer sb, String replacement) API method to append to a StringBuffer the result of a Matcher. To use a StringBuffer to append a Matcher’s result one should perform the following steps: Compile a given String regular expression to a Pattern, using compile(string regex) API method of Pattern. Use matcher(CharSequence input) ...

Read More »