CN Lab-3
CN Lab-3
Threads
Threads: A thread in Java is the direction or path that is taken while a program is being
executed (In other word : Thread is lightweight sequential flow of controls within a
program).
Generally, all the programs have at least one thread, known as the main thread, that is
provided by the JVM at the starting of the program’s execution; the main() method is
invoked by the main thread.
Threads allows a program to operate more efficiently by doing multiple things at the
same time.
Threads can be used to perform complicated tasks in the background without
interrupting the main program.
Analogy: When we use Microsoft word, at the same time, one thread controls input, one
thread control grammar checking, one thread can be used for justification and so on.
09/03/2024
SOA, Bhubaneswar
Client Client
? S er ver t h read
09/03/2024
SOA, Bhubaneswar
09/03/2024
SOA, Bhubaneswar
09/03/2024
SOA, Bhubaneswar
Thread Class:
Thread class provide constructors and methods to create and perform operations on a
thread. Thread class extends Object class and implements Runnable interface.
Constructor is a block of codes similar to the method. It is called when an instance of the
class is created.
At the time of calling the constructor, memory for the object is allocated in the memory.
It is a special type of method that is used to initialize the object.
Commonly used Constructors of Thread class:
• Thread()
• Thread(String name)
• Thread(Runnable r)
• Thread(Runnable r, String name)
09/03/2024
SOA, Bhubaneswar
09/03/2024
SOA, Bhubaneswar
09/03/2024
SOA, Bhubaneswar
09/03/2024
SOA, Bhubaneswar
09/03/2024
SOA, Bhubaneswar
Solution: Callbacks
The infinite loop that repeatedly polls each thread to see whether it’s finished can be
eliminated.
The trick is that rather than having the main program repeatedly ask each thread
whether it’s finished, you let the thread tell the main program when it’s finished.
It does this by invoking a method in the main class that started it.
This is called a callback because the thread calls its creator back when it’s done.
This way, the main program can go to sleep while waiting for the threads to finish and
not steal time from the running threads.
A callback can be executed either synchronously or asynchronously. In the case of a
synchronous callback, one function is executed right after another. In the case of an
asynchronous callback, a function is executed after an undetermined period of time and
happens in no particular sequence with other functions
09/03/2024
SOA, Bhubaneswar
Solution: Callbacks
Define the methods in an interface that we want to invoke after callback.
Define a class that will implement the callback methods of the interface.
Define a reference in other class to register the callback interface.
Use that reference to invoke the callback method.
interface ResultCallback { // Define a callback interface
void onResult(String result); }
public class CallbackExample {
class WorkerThread extends Thread { // Thread class that performs public static void main(String[] args) { // Create a callback implementation
some work and invokes the callback ResultCallback callback = result -> { // Process the result in the main thread
private ResultCallback callback; System.out.println("Received result in the main thread: " + result); };
public WorkerThread(ResultCallback callback) { WorkerThread workerThread = new WorkerThread(callback); // start the worker
this.callback = callback; } thread with the callback
public void run() { // Simulate some work workerThread.start();
try { try { workerThread.join(); // Wait for the worker thread to complete
Thread.sleep(2000); // Sleep for 2 seconds to simulate work } catch (InterruptedException e) {
} catch (InterruptedException e) { e.printStackTrace(); }
e.printStackTrace(); } System.out.println("Main thread continues executing.");
String result = "Thread result data"; // // Perform the actual work }}
callback.onResult(result); // Invoke the callback with the result
}}
09/03/2024