Posts

Showing posts from 2023

Thread , Runnable and ExecutorService

Image
  Previous approach to create a thread.  1.  Directly create an object of Thread Class after overriding the public void run() method of Thread class and call the start method to initiate the thread package com.executor; public class OldThread implements Runnable { private String task ; public OldThread() { super (); } public OldThread(String task ) { super (); this . task = task ; } @Override public void run(){ System. out .println( " Old thread runnable " + this . task ); try { Thread. sleep (2000); } catch (InterruptedException e ) { e .printStackTrace(); } System. out .println( this . task + " completed." ); } } 2. Implement the Runnable interface  and override the public void method and pass the reference of Runnable Object reference  into Thread(Runnable) constructor. call the start method to initiate the thread package com.executor; public cl...

ExecutorService with newFixedThreadPool example

The Executor Framework provides different types of executors. Some of them are .. SingleThreadExecutor FixedThreadPool(n) CachedThreadPool ScheduledExecutor Java ExecutorService is an interface in java. It allow in maintaining the thread pool and assign the tasks. Threads execute asynchronously. package com.threads.example; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** this is just name of the class. we are using the Callable interface for creating the task */ public class CallableExample { public static void main(String[] args ) throws InterruptedException, ExecutionException { /** Getting the number of cpu core and creating the thread pool according to cpu count */ int c = Runtime. getRuntime ().availableProcessors(); ExecutorSe...

Java Record : new way to create a class

Records were introduced in Java 14. They were added as a preview feature in Java 14 and became a standard feature starting from Java 16.  In Java 14, records were available as a preview feature, which means they were not considered final and could undergo changes based on developer feedback. However, the concept and basic functionality of records remained the same when they became a standard feature in Java 16. Record is class like construct. public record Person (String name, int age) { // You can also add custom methods here if needed } In the example above, we've defined a Person record with two components: name and age . The record keyword automatically generates the following for you: A constructor that takes arguments for all the components. Getter methods for each component (e.g., getName() and getAge() ). equals() , hashCode() , and toString() methods, which are based on the record's components. In Java, a record is a new feature in...

NodeJS vs Java

  There is no one-size-fits-all answer to the question of whether Node.js or Java is better for backend development. Both Node.js and Java have their strengths and weaknesses, and which one is better for a particular project depends on several factors, such as the project's requirements, team's expertise, performance needs, scalability, and budget. Node.js is a popular choice for developing real-time web applications that require a high degree of interactivity and responsiveness. It excels in handling multiple concurrent connections, making it an ideal choice for building chat applications, gaming platforms, and social networks. Additionally, Node.js uses JavaScript, which is a language that many developers are already familiar with, making it easy to learn and use. Java, on the other hand, is a widely used language in the enterprise world, and many big companies rely on it for developing complex, high-performance applications. It's a compiled language, which means it can o...

JPA mappedBy and JoinColumn

Image
In database we have a foreign key.  In simple foreign key establish the relationship between two database tables. Primary key of one table is placed into another table.  That key is called  foreign key. Here can have a book table and author table . Every author is linked with those books which are written by him.  So inside the book there is author name. that reflect the relation.  In relationship database model. Author is identified by his unique identification, This is the primary key of author table.  Book table will contain the identification of author. Book table have author id and this foreign key is not unique in book table. One single author can write multiple books, @JoinColumn specifies the foreign key column is the Book table that links to the Author entity. In this case, it's author_id. A n example of how to use @JoinColumn annotation with @ManyToOne and @OneToMany relationships in JPA. Consider two entities Author and Book , where an author...