Java Threads – an Introduction for all Skill Levels

Today we are talking about threads in Java. Threads are an essential means to implement concurrent algorithms and are therefore a key part of efficient everyday data processing. Imagine having a webserver without threads serving only one customer at a time: Every customer would need to pull a number for every single web request they make and our online shop would have a bad time.

So let’s head into it!

In modern days – Java threads with Lambda expressions

Since Java 8 we have been blessed with Lambda expression. Basically Lambda expressions serve the purpose of unnamed functions and are written as shown here:

(param1, param2, [...], paramN) -> {
    statement1; 
    statement2;
    [...]
    statementN;
}

If you just need one statement, it looks even better:

(param1, [...], paramN) -> statement

By harnessing their power we are able to write beautiful first level function style concurrent algorithms.

Let’s assume we want to sort a list with an even number of integers. We know that all ints in the second half of the list are greater than the greates number in the first half. Then we can do this:

Try it out!

Notice that we feed the threads in our Java code with Lambdas to tell them what to do. Neat, isn’t it?

Legacy Java Threads with Runnable instances

And how has it been done in the past? When I started programming with Java around 5 and 6 we had to declare a full-blown Runnable-instance that we subsequently fed to the thread. Runnable is an interface type which means that during declaration we also had to implement it’s signature method run(). This method finally contains the functionality that the thread is supposed to run. Let me show what I mean:

Try it out!

It was a bloated mess, but since it’s likely in use in modern day code, we got to list it here.

Released in Java 21: Virtual Threads

Virtual threads are part of Project Loom that aims at bringing lightweight and high-throughput concurrency models to the JVM. These virtual threads don’t run on kernel but on JVM level and thus are fully managed by the JVM. That means you as the coder do not need to keep track of them at all. The JVM will do that for you while you can rely on the coding APIs you already know and love. That is because virtual threads are created almost the same way as the native threads we have seen earlier. Let me show what I mean:

As shown above you the virtual threads API hands you a Thread instance that you can use in ways you are already familiar with. Here we wait for our virtual threads to terminate.

Another important advantage over conventional threads is their ability to scale. Since the JVM manages both ressources and lifetime of virtual threads it is done in a thoroughly optimized way. In addition, because these threads run directly in the JVM instead of the OS kernel, we can omit a lot of OS ressource management overhead. Both factors result in a significant increase of throughput.

Conclusion

So long. I hope I could help you effectively processing data by leveraging the power of Java threads. Please note that the examples shown above are optimized for the topic’s illustration only. Your way to sort a list of integers is certainly much better. If you’d like to read more about me doing Java, I have a post for you, where I talk about web test automation with Java. If you are already familiar with that and want to try something new, I recommend trying fuzz testing in Java. You will gain a fresh view on test automation and honestly it’s a blast to bomb an application with just random stuff.

Last but not least, if you want to dive in really deep into the world of Java threads, check out this article about Thread Pooling. It is quite advanced, but intuitive and powerful once you get the hang of it.

You have more questions regarding Java, me or anything else? As usual, feel free to drop me a line down below. I read every single one of your comments. Have a great day!

Home » coding
Share it with:

Character array is missing „e“ notation exponential mark

In my previous article about Fuzzing in Java, I encountered an interesting NumberFormatException: „Character array is missing „e“ notation exponential mark“. That one sparked a lot of curiosity. Today, let’s take a closer look at what exactly this error message means.

What Does This NumberFormatException Mean?

Let’s start with a simplified version of a method, dollar2euro, which converts a USD input to EUR:

Now, suppose we call this method using the string "뤇皽" as input.
This would produce the following error:

Input: 뤇皽
java.lang.NumberFormatException: Character 뤇 is neither a decimal digit number, decimal point, nor "e" notation exponential mark.

The message tells us that the character 뤇 is neither a decimal digit, a decimal point, nor an „e“ used in exponential notation. In mathematics and programming, exponential notation expresses numbers like 1e3 to represent 1000 compactly. Java expects input strings for BigDecimal to contain only valid number representations — digits, decimal points, or the character e for exponents.

Since „뤇“ is none of these, Java immediately throws a NumberFormatException.

How Can You Avoid This Error?

To prevent this error, we should ensure that the value passed to the BigDecimal constructor is a valid number.
Ideally, you would work directly with numeric types like double or int whenever possible.

In our method, this would only require a small change to the parameter type and the parsing logic.

If you still prefer to work with String inputs, make sure the string represents a valid number format.
An example of a valid input would be:

"1337.012342

By sticking to properly formatted input, you can avoid NumberFormatException issues like the one described here.

One side note here: Proper validation of user input is a critical security measure recommended by OWASP to prevent injection vulnerabilities and ensure application robustness. Using and handling this exception may help you here depending on your use case.

Conclusion

I hope this article helped clarify what the error „Character array is missing ‚e‘ notation exponential mark“ means in Java. While today’s example was constructed for clarity, such issues often arise unexpectedly when dealing with user input or external data sources.

If you have encountered a similar error in a real-world scenario, feel free to share it in the comments. I would be happy to take a look!

If you’re interested in diving deeper into testing and automation, you might enjoy my tutorials on Selenium with Java and Selenium with Python.
Or, if you want something a little more adventurous, check out my Cucumber in Rust tutorial.

Happy testing and have a great day!

Home » coding
Share it with: