JavaRush /Java Blog /Random EN /Coffee break #159. Life cycle of a thread in Java. Functi...

Coffee break #159. Life cycle of a thread in Java. Functional Interfaces and Default Methods in Java 8

Published in the Random EN group

Lifecycle of a Thread in Java

Source: Medium This article discusses the different lifecycle states of threads in Java. Coffee break #159.  Life cycle of a thread in Java.  Functional Interfaces and Default Methods in Java 8 - 1The life cycle of a thread in Java mainly consists of transitions into various states, starting with thread birth and ending with thread termination. A thread is ready to run when it is created and started by calling the start() method on the Thread class . When the Thread class's sleep() or wait() method is called , the thread becomes unusable. To manage threads programmatically, it is important to identify the state of the thread. Let's understand how the life cycle of a Java thread determines its state. Here's what the Java documentation says about this: A thread is a program execution path that can enter one of five states during its life cycle:
  1. New
  2. Runnable
  3. Running
  4. Blocked
  5. Dead
1. New (newborn state) occurs when you create a Thread object in the Thread class. The flow is created and is in a “newborn” state. That is, when a thread is created, it enters a new state, but the start() method has not yet been called on the instance. 2. Runnable. This state means that the thread is ready to run. When the start() method is called on a new thread, it becomes ready to start. In this state, the thread waits until the processor becomes available (CPU time). That is, the thread becomes a queue (series) of threads waiting to be executed. 3. Running (running state). Executing means that the processor has allocated a time slot for the thread to execute. This is the state in which the thread performs its actual function. 4. Blocked (blocked state). A thread is in a blocked state when it pauses, sleeps, or waits for some time to satisfy a given condition. 5. Dead State. This condition occurs when the run() method finishes executing instructions. The thread automatically stops or goes into the Dead State. In other words, when a thread exits the run() method , it either terminates or becomes dead.

Functional Interfaces and Default Methods in Java 8

Source: Medium In this article, you will learn about some of the changes introduced in Java 8, namely functional interfaces and default methods.

What are Default Methods?

We know that abstract classes can have abstract and non-abstract methods (methods that are implemented), but interfaces are 100% abstract (all these methods only have a signature and no implementation). But what if we need an interface instead of a class and common functionality among all implemented classes? Because then the implementation code is repeated again and again in each class that implements the interface. To avoid this, Java 8 added the default keyword . If we have a method that is implemented in an interface, then the default keyword must be assigned to that method, otherwise we will get a runtime error.

What is a functional interface?

Functional interfaces are nothing but interfaces with one abstract method and any number of standard or static methods. An example of a functional interface is Comparable, which has one abstract method, compareTo() . In turn, Runnable has a run() method and so on. We can also represent a functional interface using the @FunctionalInterface annotation. It ensures that an interface has only one abstract method:
@FunctionalInterface
public interface MyfunctionalInterface{
   public void myabstractmethod();
   public static void method1(){
       //Имплементация
    }
   public default void method2(){
       //Имплементация
    }
}

Types of functional interfaces

Consumer - takes one argument and returns nothing.
  • The three variants of Consumer are DoubleConsumer, IntConsumer and LongConsumer (depending on the type of arguments they accept).
  • Bi-Consumer - takes two arguments and returns nothing.
Predicate - takes one argument and returns a boolean value.
  • The three variants of Predicate are DoublePredicate, IntPredicate and LongPredicate (depending on the type of arguments they accept).
  • Bi-Predicate - takes two arguments and returns a boolean value.
Function - takes one argument and returns one value.
  • Many versions of functions are available as argument types, with int , long and double being the most commonly used.
  • Bi-Function - takes two arguments and returns one value.
  • The Unary and Binary operators implement Function and Bi-Function respectively, but the additional condition here is that the input and output must be identical and of the same type.
Supplier - Does not accept any input but returns a single value. Various versions of Supplier: BooleanSupplier, IntSupplier, LongSupplier and DoubleSupplier, for which return types are the corresponding primitives.
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION