07 August 2023

Multithreading In Java - learngreen.net

  

Multithreading In Java

  The multithreading functionality of Java enables the concurrent execution of several threads. A thread, a tiny sub-process, is the smallest processing unit. Both multiprocessing and multithreading are used to multitask. However, we favor multithreading over multiprocessing because threads use a shared memory area. By not allocating distinct memory regions, they save memory and the procedure is faster when changing context between threads. The two biggest industries using Java multithreading are animation and video gaming.

Multithreading is the process of running several jobs (also known as threads) simultaneously. The simultaneous execution of two or more software components is made possible by multithreading, which maximizes CPU time. A multithreaded program has two or more active components at once. It enables programmers to create code that supports the running of numerous processes concurrently inside of a single application.

The concept of multithreading in computer science refers to the independent execution of many threads (smaller units of a process) within a single process. The program can carry out numerous tasks at once since each thread denotes a distinct flow of execution.

Advantages of Multithreading:-

Performance Gains:- Since numerous threads can run concurrently on multi-core computers, multithreading can result in greater CPU resource usage. Tasks may be completed more quickly as a result of this.

A responsive application can be maintained even when some activities take a long time to complete thanks to multithreading, which enables other threads to keep working.

Resource sharing makes it simpler for various sections of the program to interact with one another and work together by allowing threads within a process to share resources like memory, file handles, and more.

Creating Threads:-

Extending Thread Class:-




  package Java;
 class CT {
 public void debug() {
 for (int i = 0; i <= 3; i++) {
 System.out.println
 ("CT " + Thread.currentThread().getId() + " " + i);
        }
    }
 }
 class MT extends CT {
 public void debug() {
 for (int i = 0; i <= 3; i++) {
 System.out.println
 ("MT " + Thread.currentThread().getId() + " " + i);
        }
    }
 }
 public class TD {
 public static void main(String[] args) {
 MT obj = new MT();
 MT obj1 = new MT();
 //Create and start the first thread
 Thread thread1 = new Thread(() -> {
 obj.debug();
 //Call the debug method of MT class for thread1
        });
 //Create and start the second thread
 Thread thread2 = new Thread(() -> {
 obj1.debug();
 //Call the debug method of MT class for thread2
        });
 //Start both threads
 thread1.start();
 thread2.start();
    }
}

 



  //Implementing Runnable Interface:-
  package Java;
/* Define a class that implements
 * the Runnable interface
 */
 class MRBL implements Runnable {
 /* Implement the run method
  * required by Runnable interface  	 
  */
 public void run() {
 // Loop to print thread ID and loop index	 
 for (int i = 1; i <= 3; i++) {
 System.out.println
 ("Thread " + Thread.currentThread().getId()+ " "+i);
	        }
	    }
	}
 public class RED {
 public static void main(String[] args) {
 // Create an instance of the MRBL class	 
 MRBL obj = new MRBL();
/* Create two threads, both sharing
 * the same MRBL instance
 */
 Thread thread1 = new Thread(obj);
 Thread thread2 = new Thread(obj);
//Start both threads
 thread1.start();
 thread2.start();
	}
  }

 The thread reads:-

Throughout their lifetime, threads transition through various states:-

Runnable threads are prepared to operate and may already be running or just waiting for CPU time.

Block:- The thread is unable to enter a synchronized block because it is awaiting a monitor lock (synchronization).

Waiting:- The thread is patiently waiting for another thread to complete a certain task.

The thread is waiting for a certain period of time while time waiting.

Terminated:- The thread's execution has come to an end.

Synchronization of threads:-

You must take care to prevent interfering with execution when several threads share resources. Typical synchronization mechanisms are as follows:

The term "synchronized" is used to describe methods or blocks that can only be accessed by one thread at a time.

For coordination and inter-thread communication, use wait( ) and notify( )/notifyAll( ). Threads have the ability to wait for a condition to be satisfied and alert other threads that are also waiting when the condition changes.

Pools of Threads:-

It might be expensive to create and manage threads for each task. Thread generation costs are decreased by using thread pools, which let you reuse a pool of worker threads for a variety of tasks.



  package Java;
  public class ESE {
 public static void main(String[] args) {
 // Create an instance of the ESE class
 ESE obj = new ESE();
 // Submit tasks using threads
 for (int i = 1; i <= 10; i++) {
 final int taskId = i;
 // Store the current task ID for each iteration
 Thread thread = new Thread(new Runnable() {
 public void run() {
 obj.executeTask(taskId);
	      }
  });
 thread.start();
	        }
	    }
 public void executeTask(int taskId) {
 System.out.println
 ("Task " + taskId + "is being executed by Thread"
 + Thread.currentThread().getId());
 // Simulate some work being done by the thread
 try {
 Thread.sleep(1000);
 } catch (InterruptedException e) {
 e.printStackTrace();
	        }
 System.out.println
 ("Task " + taskId + " completed.");
	    }
	}

 Thread Security:-

You must provide thread safety when several threads access common resources to avoid data loss or inconsistent states. Thread safety can be attained by employing methods like synchronization and thread-safe data structures.

Typical Multithreading Problems

Race conditions:- Occur when multiple threads attempt to modify the same shared data at the same time, resulting in unpredictable results.

Deadlocks:- This takes place when two or more threads are held up indefinitely while they wait for one another to release resources.

When a thread is unable to regularly access shared resources, it experiences starvation and becomes immobile.

Livelock:- A situation in which two or more threads keep responding to one another's actions while moving in the opposite direction.








No comments:

Post a Comment