Skip to main content

Posts

Showing posts with the label Unit 6 - Multithreading and Networking

Socket (TCP & UDP) communication in Java

Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**:    - The server creates a `ServerSocket` object to listen for incoming connections on a specific port.    - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client.    - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer {     public static void main(String[] args) throws IOException {         ServerSocket serverSocket = new ServerSocket(12345);         System.out.println("Server started. Waiting for client...");         Socket clientSocket = serverSocket.accept();         System.out.println("Client connected.");         BufferedReader in = new Bu

Socket (TCP & UDP) communication in Java

Socket communication in Java enables communication between two endpoints over a network. There are two main types of sockets: TCP sockets and UDP sockets. Let's explain both types with examples: TCP Socket Communication: 1. **Server Side**:    - The server creates a `ServerSocket` object to listen for incoming connections on a specific port.    - When a client connects, the server accepts the connection and creates a `Socket` object to communicate with the client.    - The server reads from and writes to the socket's input and output streams to communicate with the client. import java.io.*; import java.net.*; public class TCPServer {     public static void main(String[] args) throws IOException {         ServerSocket serverSocket = new ServerSocket(12345);         System.out.println("Server started. Waiting for client...");         Socket clientSocket = serverSocket.accept();         System.out.println("Client connected.");         BufferedReader in = new Bu

Thread interruptions and synchronization

 Thread interruptions and synchronization are important concepts in Java concurrency for managing and controlling the execution of threads in a multi-threaded environment. Thread Interruptions: 1. Interrupting Threads:    - Java provides a mechanism to interrupt a thread's execution using the `interrupt()` method.    - When a thread is interrupted, it receives an `InterruptedException` which can be caught and handled. 2. Handling Interruptions:    - Threads can check whether they have been interrupted using the `interrupted()` method or `isInterrupted()` method.    - They can respond to interruptions by gracefully stopping their execution or cleaning up resources. 3. Interrupting Thread Execution:    - Interrupted threads should clean up resources and terminate their execution in a controlled manner. Synchronization: 1. Thread Safety:    - Synchronization ensures that multiple threads can safely access shared resources without interference or data corruption.    - It prevents race

Thread scheduling and priority

Thread scheduling and priority in Java determine the order in which threads are executed by the CPU. Java provides a way to specify the priority of threads to influence their scheduling, although the exact behavior depends on the underlying operating system. Here's an overview of thread scheduling and priority in Java: Thread Scheduling: 1. Preemptive Scheduling:    - Operating systems use preemptive scheduling to switch between threads based on priority and time-slicing.    - Higher priority threads are given preference over lower priority threads.    - Threads with the same priority are scheduled in a round-robin fashion. 2. Time-Slicing:    - The CPU allocates a small time slice to each thread, and then switches to the next thread in the queue.    - Time-slicing ensures that each thread gets a fair share of CPU time. Thread Priority: 1. Thread Priority Levels:    - Java assigns each thread a priority level ranging from 1 to 10.    - The default priority level is 5.    - Use the

Creating Threads

 In Java, there are two primary ways to create threads: 1. Extending the `Thread` class: You can create a new class that extends the `Thread` class and override its `run()` method to define the code that the thread will execute. 2. Implementing the `Runnable` interface: You can create a class that implements the `Runnable` interface and provide the implementation for its `run()` method. Then, you can pass an instance of this class to a `Thread` object. Let's see examples for both approaches:  1. Extending the `Thread` class: class MyThread extends Thread {     public void run() {         // Code to be executed by the thread         for (int i = 0; i < 5; i++) {             System.out.println("Thread: " + i);             try {                 Thread.sleep(1000); // Pause execution for 1 second             } catch (InterruptedException e) {                 e.printStackTrace();             }         }     } } public class Main {     public static void main(String[] args

Multithreading

Multithreading in Java allows concurrent execution of multiple threads within a single process. It enables programs to perform multiple tasks simultaneously, improving performance and responsiveness. Here are some key points to note about multithreading in Java: Basics of Multithreading: 1. Thread: A thread is the smallest unit of execution within a process. Java programs can have multiple threads running concurrently. 2. Main Thread: When a Java program starts, it begins executing in the main thread, which is created by the JVM. 3. Creating Threads:    - Extending the `Thread` class.    - Implementing the `Runnable` interface. 4. Thread States: Threads can be in different states such as `NEW`, `RUNNABLE`, `BLOCKED`, `WAITING`, `TIMED_WAITING`, and `TERMINATED`. Thread Synchronization: 1. Race Conditions: When multiple threads access shared resources concurrently, it may lead to race conditions and inconsistent behavior. 2. Synchronization:    - Ensures that only one thread can access