Java Method Reference

Method reference is supported in Java 8. A method reference is described using :: (double colon) symbol.

Types of Method Reference
There are four types of method references:

  1. A method reference to a static method.
  2. A method reference to an instance method of an object of a particular type.
  3. A method reference to an instance method of an existing object.
  4. A method reference to a constructor.

Static Method Reference

Notice that below code between System.out and println, the :: operator is used instead of the . operator. And we don't pass arguments to the method reference. So, you can refer to static method defined in the class with interfaces that contain only one abstract method in addition to one or more default or static methods.

Consumer<?> is a functional interface. Lambda expression and Static method references that implement Consumer<String> functional interface are passed to the accept() method to be executed. Actually, Lambda expression  can be replaced with Method reference.

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class Program {
    public static void main(String[] args) {
        List<String> names = Arrays.asList("name1", "name2", "name3", "name4");


        //1. way: using Anonymous Inner Class
        Consumer<String> action1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        names.forEach(action1);

        System.out.println("*******************");

        //2. way: using Lambda Expression
        Consumer<String> action2 = (x) -> System.out.println(x);

        names.forEach(action2);

        System.out.println("*******************");


        //3. way: using Static Method Reference
        Consumer<String> action3 = System.out::println;

        names.forEach(action3);

        System.out.println("*******************");


        //4. way: pass Lambda Expression as argument
        names.forEach((x) -> System.out.println(x));


        System.out.println("*******************");


        //5. way: pass Static Method Reference as argument
        names.forEach(System.out::println);

    }
}

Output:

name1
name2
name3
name4
*******************
name1
name2
name3
name4
*******************
name1
name2
name3
name4
*******************
name1
name2
name3
name4

Continue reading

Calling Synchronous Methods Asynchronously with different Patterns

The .NET Framework enables you to call any method asynchronously. Delegates enable you to call a synchronous method in an asynchronous manner. When you call a delegate synchronously, the Invoke method calls the target method directly on the current thread. If the BeginInvoke method is called, the common language runtime (CLR) queues the request and returns immediately to the caller. The target method is called asynchronously on a thread from the thread pool. The original thread, which submitted the request, is free to continue executing in parallel with the target method. If a callback method has been specified in the call to the BeginInvoke method, the callback method is called when the target method ends. In the callback method, the EndInvoke method obtains the return value and any input/output or output-only parameters. If no callback method is specified when calling BeginInvoke, EndInvoke can be called from the thread that called BeginInvoke.

There are several patterns to call a synchronous method asynchronously as shown below.

1- BeginInvoke and EndInvoke
2- WaitHandle
3- Polling
4- Callback

 

BeginInvoke and EndInvoke Pattern

As you see that first message written in console is worked in main thread (ThreadID is 1), But, DoWork() method is called asynchronously, so that, second message is worked on different thread (ThreadID is 3).

DoWork() method returns int value and without parameters, so that, BeginInvoke method shows two parameters(callback, @object) as shown in Figure-2

Notice that EndInvoke() method takes asyncResult variable returned by BeginInvoke() method and it also returns int value because of Func delegate in Figure-3.

Figure-1

Figure-2

Figure-3

Continue reading

File copying on Network showing with Asynchronous Progress Bar in Console Application

The following example demonstrate how to copy files from TcpClient to TcpListener on network asycnhronously. While copying files asynchronously, percentage of copied files are also updated asynchronously.

if cancellation is needed, application can be cancelled by Ctrl + c keys.

Continue reading

Asynchronous Progress Bar in Console Application

The following example demonstrate how to copy files from one directory to other directory asycnhronously. While copying files asynchronously, percentage of copied files are also updated asynchronously.

Notice the second picture, Total files count is 20. But, application could copy 17 files. Because, another process has been using those 3 files. I think you can solve this problem 🙂

if cancellation is needed, application can be cancelled by Ctrl + c keys. It is shown in the third picture.


Continue reading

Homebrew package manager for Mac OS and Linux

Homebrew is a package manager for Mac OS and Linux. You can find more details in http://brew.sh . You can install and uninstall software that you need easily from Command Line Interface(CLI).

There are more than 8500 packages and casks(GUI installation packages) in Homebrew.

Homebrew Cask is the installation and management of GUI macOS applications such as Atom and Google Chrome.

I also posted Chocolatey Package Manager for Windows. Reach out from the following link.

Continue reading

Creating Simple Web Project Using Nodejs, Gulp, CoffeeScript, SASS, Less, Stylus, Jade, Haml, Express Framework etc.

This article purpose is to create a web project using some popular technologies. So, after read this article, you will be informed how you can use these technologies together. (Nodejs, Gulp, CoffeeScript, SASS/SCSS, Less, Stylus, Jade, Haml, Express Web Framework, etc.). Maybe you will change point of view and start to discover more.

Firstly, I think that we should have a package manager. So, I will use Chocolatey in Windows. If you don't have in your computer, you can read https://kenanhancer.com/2016/10/27/chocolatey-package-manager-for-windows/

Continue reading