Java Streams

JAVA Streams Sum of Elements Given a list of integers, compute the sum of all numbers. // using sum List<Integer> arr = Arrays.asList(1, 4, 5, 6, 22, 3, 90, 89, 2, 1, 3, 4, 55, 6); int sum = arr.stream() .mapToInt(Integer::intValue) .sum(); // or int sum3 = arr.stream() .reduce((a,b) -> a + b) .get(); // or int sum2 = arr.stream() .reduce(0, (total, n) -> total + n, (total1, total2) -> total1 + total2); Average of Elements Given a list of integers, compute the average of all numbers. ...

2024-07-29 · 3 min · Ramesh

Java Concurrency Multithreading

Thread Thread Subclass Here is an example of creating a Java Thread subclass: public class MyThread extends Thread { public void run(){ System.out.println("MyThread running"); } } To create and start the above thread you can do like this: MyThread myThread = new MyThread(); myTread.start(); You can also create an anonymous subclass of Thread like this: Thread thread = new Thread(){ public void run(){ System.out.println("Thread Running"); } } thread.start(); Runnable Interface Implementation Java Class Implements Runnable public class MyRunnable implements Runnable { public void run(){ System.out.println("MyRunnable running"); } } Anonymous Implementation of Runnable Runnable myRunnable = new Runnable(){ public void run(){ System.out.println("Runnable running"); } } Java Lambda Implementation of Runnable Runnable runnable = () -> { System.out.println("Lambda Runnable running"); }; Starting a Thread With a Runnable Runnable runnable = new MyRunnable(); // or an anonymous class, or lambda... Thread thread = new Thread(runnable); thread.start(); The Java Memory Model Here is a diagram illustrating the call stack and local variables stored on the thread stacks, and objects stored on the heap: ...

2024-07-23 · 6 min · Ramesh

Asynchronous Programming in Java

Asynchronous Programming in Java Parallel vs. Concurrent vs. Asynchronous Parallel: Multiple tasks executed at the same time. For example, I can walk and talk at the same time. Concurrent: I can work on multiple tasks, but I can do only one task at a moment in time. For example, I can talk or drink water at a moment. Asynchronous: It just means non-blocking. For example, I can brew a coffee, and in the meantime, I could do something else. responsive preemptive performance For Async programming JavaScript used callback. Callbacks created a problems of its own like callback hell. Promise came to solve the problem. ...

2024-07-21 · 2 min · Ramesh

Aws Questions

AWS Use Case Questions You’re setting up a website for a small shop using AWS. How would you choose the right AWS tools to make sure the website stays fast and reliable, whether there are only a few visitors or a lot of people shopping at once during a big sale? To set up a website for a small shop using AWS, ensuring it stays fast and reliable regardless of traffic fluctuations, follow these steps: ...

2024-07-16 · 13 min · Ramesh

Docker

Docker Installation Storage Storage Drivers Running Images Logging Driver Docker Swarm Namespaces Control Group Image Dockerfile Multi-Stage Build Managing Image Flattening an Image Docker Installation We have two versions of Docker. Docker Community Version (Docker CE) Docker Enterprise (Docker EE) Instalation Guide Ubuntu Debian Storage By default all files created inside a container are stored on a writable container layer. This means that: The data doesn’t persist when that container no longer exists, and it can be difficult to get the data out of the container if another process needs it. A container’s writable layer is tightly coupled to the host machine where the container is running. You can’t easily move the data somewhere else. Writing into a container’s writable layer requires a storage driver to manage the filesystem. The storage driver provides a union filesystem, using the Linux kernel. This extra abstraction reduces performance as compared to using data volumes, which write directly to the host filesystem. Storage Drivers The storage driver controls how images and containers are stored and managed on your Docker host. ...

2024-07-03 · 6 min · Ramesh