Covers the power of enumand 3 design patterns that can be applied with the help of enums. Q1. Why is it a best practice to use enums over static final int or String constants? A1. 1) static final allows invalid values. … Read more ›...
Covers the power of enumand 3 design patterns that can be applied with the help of enums. Q1. Why is it a best practice to use enums over static final int or String constants? A1. 1) static final allows invalid values. … Read more ›...
Java executor framework for running multi-threaded applications concurrently, sequentially, and with a strategy design pattern to switch between sequential and concurrent execution. 1. Running concurrently
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package com.writtentest13; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * Tasks are executed concurrently in a thread pool */ public class ThreadExecutorMainCon { private ExecutorService concurrentThreadExecutor; public static void main(String[] args) { ThreadExecutorMainCon main = new ThreadExecutorMainCon(); // concurrent executor main.concurrentThreadExecutor = Executors.newCachedThreadPool();// or // newFixedThreadPool(3) List<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>(); // create dummy tasks for (int i = 1; i <= 5; i++) { tasks.add(main.createTask(i)); } // submit the tasks to the concurrentThreadExecutor try { main.concurrentThreadExecutor.invokeAll(tasks); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Completed ........."); } private Callable<Boolean> createTask(final int i) { Callable<Boolean> task = new Callable<Boolean>() { @Override public Boolean call() throws Exception { System.out.println("Performing task " + i + " on thread - " + Thread.currentThread().getName()); return true; } }; return task; } } |
The output will be
|
1 2 3 4 5 6 7 |
Performing task 1 on thread - pool-1-thread-1 Performing task 5 on thread - pool-1-thread-4 Performing task 4 on thread - pool-1-thread-1 Performing task 3 on thread - pool-1-thread-3 Performing task 2 on thread - pool-1-thread-2 Completed ......... |
You can see more than 1 thread process 5 tasks concurrently. … Read more ›...
Generics in Java can be be a bit tricky to get your head around. Hope the explanation below enhances your understanding of generics. This complements 5 Java generics interview Q&As with examples. Raw List, List <Object>, and List<?> A raw List: is a heterogeneous mixture or a mixed bag that...
Q1. Which of these are legal Identifiers in Java? a) $Ident1 b) _Ident1 c) -Ident1 d) 2Ident1 e) private f) private1 g) null h) Ident-1 i) Ident$1 j) \u00A3Ident1 k) \u00A5Ident1 A1. … Read more ›...
Q1. What are “static initializers” or “static blocks with no function names” in Java? A1. When a class is loaded, all blocks that are declared static and don’t have function name (i.e. static initializers) are executed even before the constructors are executed. … Read more ›...
Q1. Which Java interface must be implemented by a class whose instances are transported via a Web service? a. Accessible b. BeanInfo c. Remote d. Serializable A1. Answer is “d”. Q2. What is serialization? A2. … Read more ›...
Scenario 1: The Operation interface with the annotation @FunctionalInterface. This annotation ensures that you can only have a single abstract method. You can have additional default and static method implementations. Step 1: Define the interface. … Read more ›...
Q1. What are mock objects? A1. Mock objects are used in unit testing to ensure that your tests don’t fail due to volatility of the data changes. There are mocking frameworks like EasyMock, Mockito, and PowerMock. … Read more ›...
Q1. What do you understand by the terms annotation types, annotations, and annotation processors? A1. An annotation type is used for defining an annotation. Java 5 defines a number of annotation types like @Override, @SuppressWarnings, @Deprecated, etc and meta annotation types that are used by annotation (i.e. … Read more...
Q1. What is difference between ‘Executor.submit()’ and ‘Executor.execute()’ method ? A1. The difference is that execute() doesn’t return a “Future” object, so you can’t wait for the completion of the Runnable, and get any exception it throws. The submit(…) method accepts Callable<V> and returns an instance of Future<V>, … Read...
Prerequisite: Understanding of Java 8 streams.
Q. You have a list of key value pairs (“student name”, “subject”) as shown below.
Input
|
1 2 3 4 5 6 7 |
("Sam Peter", "English"), ("Sam Peter", "Science"), ("John English", "Science"), ("Neil Graham", "Maths"), ("Neil Graham", "Arts"), ("Neil Graham", "Music") |
The required task is to list all the subjects for each student as shown below.
Result
|
1 2 3 4 |
"Samp Pter" --> [English, Science] "John English" --> [Science] "Neil Graham" --> [Maths, Arts, Music] |
A.
…
Q1. What is the difference between “==” and equals(..) method when comparing 2 objects?
A1.It is important to understand the difference between identity (i.e. ==) comparison, which is a shallow comparison that compares only the object references, and the equals( ) comparison,
…
Q1. What are the different data sizes, and what technologies can be used to process them? A1. In general, data sizes can be classified as shown below. 1) Small size data is < 10 GB in multiple files. … Read more ›...
I have a good news, I got job offer. I like to share with you from my bottom of my heart feeling thankful to you.