THREAD IN JAVA

PROCESS DEFINITION

java-logo

The process is the entity used by the operating system to represent a specific execution of a program. Basically, a process is a program when it is running.

PROCESS DEFINITION

PROCESSES

A software usually consists of one or more files that are compiled to run. A software installation involves copying these files to the device. When we run software, a process is generated on the device that:

  • has a unique ID
  • occupies part of the volatile memory (the RAM)

INTRODUCTION TO THREADS AND CONCURRENCY

THREADS

We said that a process, in simpler terms, is a running program. One or more threads are executed in the context of the process. A thread is the basic unit to which the operating system allocates processor time.

I thread

Modern Operating Systems are multitasking i.e., they are capable of running more than one process at a time, although it is parallelism that gives us this optical illusion, they are also multithreaded, to give an example just open a word processing program such as Word and we notice as we are typing that a thread is handling spell checking, or we can think of a web server that accepts requests while other threads handle them. These are just a few examples.

LIFE CYCLE OF A THREAD

A thread has a well-defined life cycle that includes the following states:

  • New
  • Runnable
  • Running
  • Waiting
  • Dead
Ciclo di vita


New
: This is the initial state that a new thread is in.

Runnable: The thread is in this state when it has been started and is ready to run.

Running: is the state the thread is in when it starts its execution.

Waiting: The thread is in this state when its execution is suspended (e.g., when another thread is executed) for a specified period of time.

Dead: a thread enters this state after its execution is complete.

THREAD PRIORITY

Priority is information that tells the scheduler the level of importance of a thread.

WARNING

The order in which the CPU executes the threads is not determined a priori. However, if a number of threads are blocked and waiting to be executed, the scheduler tends to activate those with higher priority first. If there are low-priority threads, these will also be activated by the scheduler sooner or later. In this way starvation, or the inability of an execution-ready process to obtain hardware and software resources it needs, is avoided.

PRIORITY IN JAVA

The priorities of a thread in Java range from 1 to 10:

  • MIN_PRIORITY (1)
  • MAX_PRIORITY (10)

Each thread, by default has NORM_PRIORITY (5) priority.

Threads with high priority are more important and in case of need, they are activated by the scheduler before those with low priority.

CREATE A THREAD IN JAVA

To create threads in Java we have two ways:

  • create a class that extends the Thread class
  • create a class that implements the Runnable interface

The java.lang.Thread class

  • Our class must inherit the java.lang.Thread class.
  • The most important method present in this Thread class is run()
  • The run() method needs to be rewritten (you have to do the override).
  • The run() method will contain the instructions for the task to be executed.
  • The code in the run() method will be executed concurrently to other threads in a program
Creazione di thread

To start a thread the steps are:

  1. create an instance of our class that extends Thread
  2. invoke the start() method, which will take care, after the thread configuration, of invoking the run()

NOTE: the run() method cannot be invoked directly

Useful methods:

  • yield(), allows you to suggest to the scheduler to free up the CPU and make it available to some other thread
  • sleep(), allows execution to cease for a given number of milliseconds

THE RUNNABLE INTERFACE

Our class must implement the java.lang.Runnable interface.

  • It can be used when our class already inherits another class and therefore we cannot extend the Thread class (remember that Java does not support multiple inheritance!).
  • The interface specifies that you must implement the run() method
  • To create a thread, we need to use the constructor of the Thread class by passing to it as an argument the instance of our class that implements Runnable
Interfaccia Runnable

LINKS TO PREVIOUS POSTS

THE JAVA LANGUAGE

LINK TO CODE ON GITHUB

GITHUB

EXECUTION OF THE EXAMPLE CODE

  • Download the code from GITHUB, launch the JAR file with the following command in Visual Studio Code, locating in the directory containing the JAR.

        java -jar –enable-preview CorsoJava.jar

  • Or run the main found in the file CorsoJava.java.