Raja has Published 469 Articles

How do threads communicate with each other in Java?

raja

raja

Updated on 29-Nov-2023 11:11:33

6K+ Views

Inter-thread communication involves the communication of threads with each other. The three methods that are used to implement inter-thread communication in Java. wait() This method causes the current thread to release the lock. This is done until a specific amount of time has passed or another thread calls the notify() or notifyAll() ... Read More

Can a "this" keyword be used to refer to static members in Java?

raja

raja

Updated on 29-Nov-2023 10:58:08

912 Views

No, the "this" keyword cannot be used to refer to the static members of a class. This is because the “this” keyword points to the current object of the class and the static member does not need any object to be called. The static member of a class can be ... Read More

Which collection classes are thread-safe in Java?

raja

raja

Updated on 29-Nov-2023 10:31:28

13K+ Views

A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc. Stack The Stack class in Java implements the stack ... Read More

How many ways to iterate a TreeSet in Java?

raja

raja

Updated on 29-Nov-2023 10:07:15

1K+ Views

A Treeset is a subclass of AbstractSet class and implements NavigableSet Interface. By Default, a Treeset gives an ascending order of output and it will use a Comparable interface for sorting the set elements. Inside a Treeset, we can add the same type of elements otherwise it can generate a ... Read More

Object level lock vs Class level lock in Java?

raja

raja

Updated on 28-Nov-2023 09:43:01

6K+ Views

Both Object level lock and Class level lock are used to achieve synchronization mechanisms in a multi-threaded application. Object Level Lock Every object in Java has a unique lock. If a thread wants to execute a synchronized method on a given object, first it has to get a lock of that object. ... Read More

Can we call the wait() method without acquiring the lock in Java?

raja

raja

Updated on 27-Nov-2023 10:49:02

553 Views

No, we cannot call the wait() method without acquiring the lock. In Java, once the lock has been acquired then we need to call wait() method (with timeout or without timeout) on that object. If we are trying to call the wait() method without acquiring a lock, it can throw ... Read More

What is the use of Thread.sleep() method in Java?

raja

raja

Updated on 27-Nov-2023 09:23:59

5K+ Views

The sleep() method is a static method of Thread class and it makes the thread sleep/stop working for a specific amount of time. The sleep() method throws an InterruptedException if a thread is interrupted by other threads, that means Thread.sleep() method must be enclosed within the try and catch blocks ... Read More

Importance of yield() method in Java?

raja

raja

Updated on 24-Nov-2023 10:37:40

13K+ Views

A yield() method is a static method of Thread class and it can stop the currently executing thread and will give a chance to other waiting threads of the same priority. If in case there are no waiting threads or if all the waiting threads have low priority then the ... Read More

How to instantiate a static inner class with reflection in Java?

raja

raja

Updated on 24-Nov-2023 09:18:44

2K+ Views

A static inner class can be instantiated without the need for an instance of the outer class. In general, an Inner class is a part of nested class, called Non-static nested classes in Java. The types of inner classes are member inner class, anonymous inner class, and local inner class. We ... Read More

How can we implement a custom iterable in Java?

raja

raja

Updated on 23-Nov-2023 11:23:04

2K+ Views

An Iterable interface is defined in java.lang package and introduced with Java 5 version. An object that implements this interface allows it to be the target of the "for-each" statement. This for-each loop is used for iterating over arrays and collections. An Iterable interface can also be implemented to create ... Read More

Advertisements