In java, join() methods allows one thread to wait for the complete execution of other thread. Consider there are two threads T1 & T2, currently thread T1 is executing, and we invoked join() method on T2 then execution of thread T1 is paused and waits for the completion of execution of Thread T2 and then…
Create Custom Annotation: In java, creating an annotation is @interface is used to create an Annotation. public @interface MyAnnotation{ } We can also define methods inside an annotation. public @interface MyAnnotation{ int value(); } Note: The methods of an annotation should adhere to the following rules: Method declaration should not have any parameters Method declaration should not…
There is no official method to kill a thread in Java. Stopping a thread is entirely managed by the JVM. Although Java provides several ways to manage the thread lifecycle such as a start(), sleep(), stop() (deprecated in Java 1.1), etc. but does not provide any method to kill a thread and free the resources cleanly. Oracle specified the reason for deprecating the…
Thread in Java Table Of Contents Getting Thread Name Setting Name to Thread Using Thread Constructor Using Thread.setName() 1. Getting Thread Name By default, the Java compiler sets a default name of each thread while creating, and we can get the thread name by using the Thread.currentThread().getName() method. In the following example, we created a Thread by implementing the Runnable interface and…
Table Of Contents Creating a New Thread By Extending Thread Class By Implementing Runnable Interface Using Lambda Expressions Starting a New Thread Using Thread.start() Using ExecutorService Delayed Execution with ScheduledExecutorService Using CompletableFuture 1. Creating a New Thread In Java, we can create a Thread in the following ways: By extending Thread class By implementing Runnable interface Using Lambda expressions 1.1. By…
Java concurrency is a pretty complex topic and requires a lot of attention while writing application code dealing with multiple threads accessing one/more shared resources at any given time. Java 5, introduced some classes like BlockingQueue and Executors which take away some of the complexity by providing easy to use APIs. Programmers using concurrency classes will feel a lot…
In simple words, concurrency is the ability to run several programs or several parts of a program in parallel. Concurrency enables a program to achieve high performance and throughput by utilizing the untapped capabilities of the underlying operating system and machine hardware. For example, modern computers have several CPUs or several cores within one CPU, the program…
In Java, a synchronized block of code can only be executed by one thread at a time. Also, java supports multiple threads to be executed concurrently. This may cause two or more threads to access the same fields or objects at the same time. Synchronization is the process which keeps all concurrent threads in execution to…
Defining thread safety is surprisingly tricky. A quick Google search turns up numerous “definitions” like these: Thread-safe code is code that will work even if many Threads are executing it simultaneously. A piece of code is thread-safe if it only manipulates shared data structures in a manner that guarantees safe execution by multiple threads at the same…