---
# System prepended metadata

title: Core JAVA Use cases

---

How to create Threads?


There are two ways of creating threads – one by extending the Thread class and other by creating a thread with a Runnable

example 


```
class MultithreadingDemo extends Thread {
    public void run()
    {
        try {
            // Displaying the thread that is running
            System.out.println(
                "Thread " + Thread.currentThread().getId()
                + " is running");
        }
        catch (Exception e) {
            // Throwing an exception
            System.out.println("Exception is caught");
        }
    }
}
 
// Main Class
public class Multithread {
    public static void main(String[] args)
    {
        int n = 8; // Number of threads
        for (int i = 0; i < n; i++) {
            MultithreadingDemo object
                = new MultithreadingDemo();
            object.start();
        }
    }
}
```

by using runnable interface 

```
class MultithreadingDemo implements Runnable {
    public void run()
    {
        try {
            // Displaying the thread that is running
            System.out.println(
                "Thread " + Thread.currentThread().getId()
                + " is running");
        }
        catch (Exception e) {
            // Throwing an exception
            System.out.println("Exception is caught");
        }
    }
}
 
// Main Class
public class Multithread {
    public static void main(String[] args)
    {
        int n = 8; // Number of threads
        for (int i = 0; i < n; i++) {
            MultithreadingDemo object
                = new MultithreadingDemo();
                Thread t = new Thread(object);
            t.start();
        }
    }
}
```

so when to use which?
Use Thread when:
You only need to manage the thread's lifecycle and don't need to extend another class.

Use Runnable when:
You need to extend another class along with implementing multithreading capabilities.
You want to create a reusable task that can be executed by multiple threads.

So what is the existing problem with thread and runnable ? 

enter callable and futures 

```
import java.util.concurrent.Callable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
class MultithreadingDemo implements Callable<String> {

    @Override
    public String call() {
        try {
            // Displaying the thread that is running
            System.out.println("Thread " + Thread.currentThread().getId() + " is running");
            return "Task result"; // You can return a value from call()
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

public class Multithread {
    public static void main(String[] args) throws Exception {
        int n = 8; // Number of threads

        // Create an ExecutorService (e.g., fixed thread pool)
        ExecutorService executor = Executors.newFixedThreadPool(n);

        // Array to store Future objects
        Future<String>[] futures = new Future[n];

        // Submit tasks (Callable objects) to the ExecutorService
        for (int i = 0; i < n; i++) {
            MultithreadingDemo task = new MultithreadingDemo();
            futures[i] = executor.submit(task);
        }

        // Optionally retrieve results from the Future objects
        for (Future<String> future : futures) {
            try {
                String result = future.get(); // Blocks until the task finishes
                System.out.println("Result from thread " + future.get() + ": " + result);
            } catch (InterruptedException  e) {
                e.printStackTrace();
            }
        }

        // Shutdown the ExecutorService (optional)
        executor.shutdown();
    }
}
```

interview questions
 ```
 Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34}
Output: [59, 13]

Input: Stream = {5, 13, 4, 21, 27, 2, 59, 34}
Output: []

method 1 

import java.util.*;
import java.util.stream.*;  
public class Duplicate {   
    public static <T> Set<T>
    findDuplicateInStream(Stream<T> stream)
    {      
            
    }  
    // Driver code
    public static void main(String[] args)
    {
  
        Stream<Integer> stream
            = Stream.of(5, 13, 4,
                        21, 13, 27,
                        2, 59, 59, 34); 
       
        System.out.println(
            findDuplicateInStream(stream));
    }
}

 ```

******************************************

```
import static java.util.stream.Collectors.*; 
import java.lang.*;
import java.util.*;
import java.util.stream.*;
import java.util.stream.Collectors;
class sortmapKey {    
    static Map<String, Integer> map = new HashMap<>(); 
   public static void sortbykey()
    { 
    }
    public static void main(String args[])
    {
        map.put("Ketan", 80);
        map.put("Rishi", 90);
		 map.put(null, 1);
		 map.put(null, 2);
        sortbykey();}}
```






//////////////////////////////////////////////

was able to code and explain the logic behind the code yes /no
Was able to explain basic OOPS concept yes /no
was able to explain a debugging steps yes /no
Was able to explain concepts of Spring , Rest API yes /no
Feels like has working experience in real project  yes /no


Was able to communicate  yes /no
was able to understand the question yes /no
English looks good yes /no
was answering to the point yes /no

On time for interview yes /no
Was eager towards the interview yes /no
Smiled in an interview yes /no





