Java 8 Lambdas limitations: closures

Suppose we want to create a simple thread that only prints something on the console:

int answer = 42;
Thread t = new Thread(
  () -> System.out.println("The answer is: " + answer)
);

What if we changed the value of answer while the thread is executing?

In this article, I would like to answer this question, discussing the limitations of Java lambda expressions and consequences along the way.

The short answer is that Java implements closures, but there are limitations when we compare them with other languages. On the other hand, these limitations can be considered negligible.

To support this claim, I will show how closures play an essential role in a famous language as JavaScript.

Read more

Convert a SID to String with Java

A security identifier (SID) is an unique identifier, commonly used in Microsoft’s systems. For example, it’s used to identify users within Windows, or, more generally, within an Active Directory.

The SID is a binary value, with a variable length, that can be also be represented as a string. This conversion is implemented by the function called ConvertSidToStringSid, provided by the library Advapi32.dll, available only in Windows.

Hence, if you need to perform this conversion, you can procede with one of these two paths:

  • Use Advapi32 (only in Windows)
  • Rewrite the conversion
Thus, it’s very useful to have that implementation in a particular language (in our case Java), to be used everywhere. I noticed that, after a research in Internet, all the implementations are wrong, despite they work in general. Therefore, I decided to write my own implementation, thoroughly tested to prove you its correctness.

Read more

Integrating PeerSim with Eclipse

PeerSim, if you don’t know it, is a famous P2P networks simulator, written in Java. You can find its home clicking this link.

Surely, when you are writing in Java, it’s convenient to use Eclipse. In this article I will talk about how to configure eclipse to work with PeerSim. I decide to write this article mainly because I’ve founded some articles that talk about this integration; but I disagree with their solutions.

Read more

Artificial Intelligence: Markov Decision Process

In AI, sometimes, you need to plan a sequence of action that lead you to your goal. In stochastic environment, in those situation where you can’t know the outcomes of your actions, a sequence of actions is not sufficient: you need a policy.

Markov Decision Process is a mathematical framework that helps to build a policy in a stochastic environment where you know the probabilities of certain outcomes.

In this post, I give you a breif introduction of Markov Decision Process. Then, I’ll show you my implementation, in python, of the most important algorithms that can help you to find policies in stocastic enviroments. You can fine a more detailed description of the Markov Decision Process in my my slides that I’ve used for a seminar at University.

Read more

MExpr

Finally, I release my first version of MExpr library!

A C++ Library that parses mathematical expressions. The library is made using bison and flex. Evaluation on the AST Tree browsing it recursively. Ability to compile the expression in a bytecode that uses a stack to be evaluated. Support for variables, for implicit multiplications like -3xy^2, for functions and for overloaded functions. Furthermore, there is a set of standard functions taken from math.h.

See more…

ExitWindowsEx in C#

I post you my simple class to shutdown, reboot and logoff Windows using the famous ExitWindowsEx. Each method is overloaded with another that force the action, in other words, it executes the action without prohibiting¬†Windows to send the WM_QUERYENDSESSION message. With the latter way, the user can’t cancel it.

Read more

Hide Console Window in C#

I just made a silent application to check how much time my brother stay on his computer. But, I needed to make an application that doesn’t show any window. Now, I show you my personal solutions to do this for the Console Application Projects and for the Windows Forms Application Projects.

Read more