WHAT ARE THREADS IN JAVA ??

What are threads?

Guided by Bipin Ajay , Founder of Tech Jitendra

Nearly every operating system supports the concept of processes — independently running programs that are isolated from each other to some degree.

Threading is a facility to allow multiple activities to coexist within a single process. Most modern operating systems support threads, and the concept of threads has been around in various forms for many years. Java is the first mainstream programming language to explicitly include threading within the language itself, rather than treating threading as a facility of the underlying operating system.

Threads are sometimes referred to as lightweight processes. Like processes, threads are independent, concurrent paths of execution through a program, and each thread has its own stack, its own program counter, and its own local variables. However, threads within a process are less insulated from each other than separate processes are. They share memory, file handles, and other per-process state.

A process can support multiple threads, which appear to execute simultaneously and asynchronously to each other. Multiple threads within a process share the same memory address space, which means they have access to the same variables and objects, and they allocate objects from the same heap. While this makes it easy for threads to share information with each other, you must take care to ensure that they do not interfere with other threads in the same process.

The Java thread facility and API is deceptively simple. However, writing complex programs that use threading effectively is not quite as simple. Because multiple threads coexist in the same memory space and share the same variables, you must take care to ensure that your threads don’t interfere with each other.

Every Java program uses threads

Every Java program has at least one thread — the main thread. When a Java program starts, the JVM creates the main thread and calls the program’s main()method within that thread.

The JVM also creates other threads that are mostly invisible to you — for example, threads associated with garbage collection, object finalization, and other JVM housekeeping tasks. Other facilities create threads too, such as the AWT (Abstract Windowing Toolkit) or Swing UI toolkits, servlet containers, application servers, and RMI (Remote Method Invocation).

Why use threads?

There are many reasons to use threads in your Java programs. If you use Swing, servlets, RMI, or Enterprise JavaBeans (EJB) technology, you may already be using threads without realizing it.

Some of the reasons for using threads are that they can help to:

  • Make the UI more responsive
  • Take advantage of multiprocessor systems
  • Simplify modeling
  • Perform asynchronous or background processing

Thanking You

Tech Jitendra

Published by Tech Jitendra Direct Investing

Tech Jitendra Direct Investing (TJDI) Empoworing Your Wealth

Leave a comment

Design a site like this with WordPress.com
Get started