Found 2617 Articles for Java

Difference between scheduledThread pool and Single Thread Executor.

Himanshu shriv
Updated on 09-Sep-2020 11:16:53

1K+ Views

Sr. No.KeyScheduled Thread PoolSingle Thread Executor1BasicCreates a thread pool that can schedule commands to run after a given delay, or to execute periodically. Tasks are guaranteed to execute sequentially, and no more than one task will be active at any given time2QueueIt uses Delay Queue to store tasks. Schedule the task based on time delay.It uses blocking queue.3Thread LifetimeT he number of threads to keep in the pool, even if they are idleRecreate thread if killed because of the task.4.Thread Pool SizeIt always has a single thread running.The thread pool can grow from zero threads to Integer.MAX_VALUE5.Use CaseWe should used fixedthreadpool, ... Read More

Difference between Fixed thread pool and cached thread pool.

Himanshu shriv
Updated on 09-Sep-2020 09:41:08

3K+ Views

Executor framework are designed using thread pool concept. Thread pool is the way to reuse the already created thread instead of creating a new thread every time to execute the current task.Executors class provides a factory method to create thread pools. The ThreadPoolExecutor class is the base implementation for the executors that are returned from many of the Executors  factory methods.Sr. No.KeyFixed Thread PoolCached Thread Pool1BasicAs per Java Doc −Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional ... Read More

Difference between Executor and ExecutorServices in Java

Himanshu shriv
Updated on 09-Sep-2020 09:36:46

912 Views

Executor and ExecutorServices both interfaces are part of the Executor framework. It is released with Java 5. In java, thread creation is very expensive operation so we should  reuse the available thread instead of starting a new thread every time and we can achieve the same using Executor framework.Executor framework use the thread pool to execute the task parallel which helps to optimize response time and resource utilization. It provides four types of built in thread pools −Fixed Thread PoolCached Thread PoolScheduled Thread PoolSingle Thread ExecutorSr. No.KeyExecutorExecutorServices1BasicIt is a parent interfaceIt extends Executor Interface2MethodIt has execute() methodIt has submit() method3Return TypeIt ... Read More

Difference between Runnable and Callable interface in java

Himanshu shriv
Updated on 09-Sep-2020 09:33:21

4K+ Views

Runnable and Callable both functional interface. Classes which are implementing these interfaces are designed to be executed by another thread.Thread can be started with Ruunable and they are two ways to start a new thread: one is by subclassing Thread class and another is implementing Runnable interface.Thread class does not have constructor for callable so we should use ExecutorService  class for executing thread.Sr. No.KeyRunnableCallable1PackageIt belongs to Java.langIt belongs to java.util.concurrent2Thread CreationWe can create thread by passing runnable as a parameter.We can’t create thread by passing callable as parameter  3Return TypeRuunable does not return anythingCallable can return results4.MethodIt has run() methodIt ... Read More

Difference between Streams and Collections in Java 8

Himanshu shriv
Updated on 09-Sep-2020 09:31:05

4K+ Views

Java Collections framework is used for storing and manipulating group of data. It is an in-memory data structure and every element in the collection should be computed before it can be added in the collections.Stream API is only used for processing group of data. It does not modify the actual collection, they only provide the result as per the pipelined methods.Sr. No.KeyCollectionsStreams1BasicIt is used for storing and manipulating group of dataStream API is only used for processing group of data2PackageAll the classes and interfaces of this API is in the Java.util packageAll the classes and interfaces of this API is ... Read More

File Objects in Java

AmitDiwan
Updated on 17-Aug-2020 09:48:42

1K+ Views

The File object represents the actual file/directory on the disk. Here are the list of constructors to create File Object in Java −Sr.No.Method & Description1File(File parent, String child)This constructor creates a new File instance from a parent abstract pathname and a child pathname string.2File(String pathname)This constructor creates a new File instance by converting the given pathname string into an abstract pathname.3File(String parent, String child)This constructor creates a new File instance from a parent pathname string and a child pathname string.4File(URI uri)This constructor creates a new File instance by converting the given file: URI into an abstract pathname.Assuming an object is ... Read More

Precision Handling in Java

AmitDiwan
Updated on 17-Aug-2020 09:46:45

442 Views

Let us see how precisions are handled in Java −Example Live Demoimport java.io.*; import java.lang.*; public class Demo{    public static void main(String[] args){       double my_val = 34.909;       System.out.println("The formatted value of 34.909 is ");       System.out.println(String.format("%.7f", my_val));       double my_val_2 = 12.56;       System.out.println("The formatted value of 12.56 is ");       System.out.println(String.format("%.9f", my_val_2));    } }OutputThe formatted value of 34.909 is 34.9090000 The formatted value of 12.56 is 12.560000000A class named Demo contains the main function, where a double valued integer is declared, and it is ... Read More

Logical Operators on String in Java

AmitDiwan
Updated on 17-Aug-2020 09:45:22

509 Views

Let us implement logical operators on String in Java −Example Live Demoimport java.io.*; public class Demo{    public static void main(String[] args){       int a = 45, b = 32, c = 87, d = 1;       System.out.println("The first variable is " + a);       System.out.println("The second variable is = " + b);       System.out.println("The third variable is = " + c);       if ((a > b) && (b == c)){          d = a + b + c;          System.out.println("The sum is " + ... Read More

Complex Numbers in Java

AmitDiwan
Updated on 17-Aug-2020 09:43:26

6K+ Views

Complex numbers are those that have an imaginary part and a real part associated with it. They can be added and subtracted like regular numbers. The real parts and imaginary parts are respectively added or subtracted or even multiplied and divided.Example Live Demopublic class Demo{    double my_real;    double my_imag;    public Demo(double my_real, double my_imag){       this.my_real = my_real;       this.my_imag = my_imag;    }    public static void main(String[] args){       Demo n1 = new Demo(76.8, 24.0),       n2 = new Demo(65.9, 11.23),       temp;       ... Read More

How to check if a string is a valid keyword in Java?

AmitDiwan
Updated on 17-Aug-2020 09:41:23

891 Views

To check if a string is a valid keyword in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    static boolean valid_identifier(String my_str, int n){       if (!((my_str.charAt(0) >= 'a' && my_str.charAt(0) = 'A' && my_str.charAt(1) = 'a' && my_str.charAt(i) = 'A' && my_str.charAt(i) = '0' && my_str.charAt(i)

Advertisements