The best way to map @ManyToMany
June 29, 2021 Ravi Yasas
@ManyToMany
Student entity
Task entity
@manytomany / basics / beginners / hibernate / java / jpa / manytomany / mapping / tutorials
How to avoid NullPointerException
June 29, 2021 Ravi Yasas
java.lang.NullPointerException
When can it be thrown?
- Calling the instance method of a null object.
- Accessing or modifying the field of a null object.
- Taking the length of null as if it were an array.
- Accessing or modifying the slots of null as if it were an array.
- Throwing null as if it were a Throwable value.
How to avoid it?
- Use Optional in Java 8
- Call equals() and equalsIgnoreCase() methods on known objects.
- Use valueOf() over toString()
- Use null-safe methods and libraries. Ex: Apache Commons StringUtils.
- Use @NotNull and @Nullable annotations
- Always check for nulls.
basics / beginners / exceptions / java / java8 / nullpointer / optional / toString() / tutorials / value)f()
Thread pool in Java
June 27, 2021 Ravi Yasas
The Thread pool
Introduction
- Executor implementations use thread pools.
- The thread pool is a collection of worker threads that are ready to serve.
- Creating new threads and manage them uses a big amount of data.
- Worker threads in the thread pool will help to reduce this overhead of creating threads.
- Tasks are submitted to the pool via an internal queue called blocking queue.
- If there are more tasks than active threads, these tasks will be inserted into a blocking queue until a thread becomes available.
- A common type of thread pool is the fixed thread pool.
Fixed thread pool
- This has a specific number of running threads.
- If a thread stops, this will replace with a new thread.
- The main advantage of the fixed thread pool is it will limit the threads of an application.
- It won't let the application exceed the limit of threads that the application handles.
beginners / concurrency / executor / executor service / java / Java Concurrency API / learning / priority / thread / thread priority / threads / tutorial
Java ExecutorService
June 27, 2021 Ravi Yasas
Java ExecutorService
Introduction
- If an application has few threads, it can be used threads very easily using the above methods.
- But if an application has many threads, it will be not easy to handle this.
- Executors can be used to avoid this complexity.
- The executor framework is a framework that can be used for creating, managing and executing threads.
- This provides an asynchronous feature to Java applications.
Features of Executor service
- Thread creation
- Provides thread pool and various methods to create threads.
- Thread Management
- The thread pool will manage the life cycle of threads.
- Thread Execution
- Various methods will be provided
- Thread schedulers can be used.
Executor interfaces in the Java Concurrency API
- Executor
- Provides a single method execute() to create a thread.
- (new Thread(r)).start() can be replaced with e.execute(r)
- ExecutorService
- Also provides execute() method but this accepts both Runnable and Callable objects.
- ScheduledExecutorService
- This can be used to act as the asynchronous manner in Java
- It can be used periodically and after a specific timeout.
- Runnable and Callable tasks can be executed
beginners / concurrency / executor / executor service / java / Java Concurrency API / learning / priority / thread / thread priority / threads / tutorial
Thread priority
June 27, 2021 Ravi Yasas
Thread priority
- Each thread has a priority.
- It starts from 1 to 10 and the highest priority is 10 while the lowest is 1.
- The default priority of the main thread is 5.
- If you set the thread priority out of 10, it will give a compile-time error
List vs Queue vs Set vs Map
June 27, 2021 Ravi Yasas
List vs Queue vs Set vs Map
List | Queue | Set | Map | |
Duplicates | Yes | Yes | No | No(Allow duplicate values not keys) |
Order | Yes | Yes | No | No |
Null values | Yes | Priority queue doesn't allow, but queue using LinkedList allows null | Single null | Single null key and many null values |
beginners / coding / collection framework / collections / learning / list / map / queue / set / tutorial