Found 9150 Articles for Object Oriented Programming

How many ways to call garbage collector (GC) in Java?

Vivek Verma
Updated on 27-May-2025 15:23:58

1K+ Views

This article explains the different ways of calling the garbage collector (GC) in Java. It also includes a brief introduction to garbage collection, various calling approaches, and relevant examples. Garbage Collection (GC) in Java In Java, the garbage collection is carried out by a daemon thread called the Garbage Collector (GC). Instead of waiting until the Java Virtual Machine (JVM) runs a garbage collector, we can request the JVM to run the garbage collector. There is no guarantee that the JVM will accept our request. In Java, we can call the garbage collector (GC) manually in "two ways", which ... Read More

How can we call the invokeLater() method in Java?

raja
Updated on 02-Jul-2020 08:12:57

4K+ Views

An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread. The SwingUtilities.invokeLater() method works like SwingUtilities.invokeAndWait() except that it puts the request on the event queue and returns immediately. An invokeLater() method does not wait for the block of code inside the Runnable referred by a target to execute.Syntaxpublic static void invokeLater(Runnable target)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class InvokeLaterTest extends Object {    private static void print(String msg) {       String name = Thread.currentThread().getName();       System.out.println(name + ": " + msg);    }   ... Read More

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

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 or it must be specified with throws clause. Whenever we call the Thread.sleep() method, it can interact with the thread scheduler to put the current thread to a waiting state for a specific period of time. Once the waiting time is over, the thread changes from waiting state to runnable state. Syntax public ... Read More

Importance of join() method in Java?

Vivek Verma
Updated on 08-May-2025 14:06:51

3K+ Views

The join() Method In Java, a join() is a final method of the Thread class, and it can be used to join the start of a thread's execution to the end of another thread's execution so that a thread will not start running until another thread has ended. If the join() method is called on a thread instance, the currently running thread will be blocked until the thread instance has finished its execution. The join() method in Java is important in multithreading because it allows one thread to wait for the completion of another thread before continuing execution. Syntax Following is ... Read More

What is the purpose of overriding a finalize() method in Java?

Vivek Verma
Updated on 27-May-2025 15:07:58

1K+ Views

In Java, overriding is a technique used to provide a specific implementation of a method that is already defined in a superclass (i.e., parent class), allowing a subclass (i.e., child class) to modify or extend the behavior of that method. Purpose of Overriding the finalize() Method The purpose of a finalize() method can be overridden for an object to include the cleanup code or to dispose of the system resources that can be done before the object is garbage collected. If we are overriding this method, then we need to call the finalize() method explicitly. The finalize() method can be ... Read More

How to implement a rollover effect for a JButton in Java?

raja
Updated on 02-Jul-2020 07:04:18

1K+ Views

A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a GUI application. A JButon can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. We can implement the rollover effect when the mouse moves over a JButton by overriding the mouseEntered() method of the MouseListener interface.Syntaxvoid mouseEntered(MouseEvent e)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class RollOverButtonTest extends JFrame {    private JButton button;    public RollOverButtonTest() {       setTitle("RollOverButton Test");       button = new JButton("Rollover Button");       button.addMouseListener(new MouseAdapter() {     ... Read More

How can we avoid a deadlock in Java?

Vivek Verma
Updated on 14-May-2025 14:07:06

5K+ Views

This article explains several strategies to avoid deadlock in Java, including a brief introduction to deadlock, strategies, and respective examples. Deadlock in Java In case of multi-threading, a deadlock is a programming situation where two or more threads are holding resources needed by other threads and are blocked forever, waiting for each other (to release the required resource). A deadlock condition will occur with at least two threads and two or more resources.How To Avoid Deadlock in Java? Following is a list of strategies to avoid the deadlock in Java: ... Read More

Object.assign() in JavaScript?

vineeth.mariserla
Updated on 20-Aug-2019 14:42:35

681 Views

Object.assign()This method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'Set' on the target. It returns the target object which has properties and values copied from the target object. This method does not throw on null or undefined source values.syntaxObject.assign(target, ...source objects);It takes source objects and a target object as parameters and push the source objects into the target object and display the target object.Example-1In the following example, the properties from the source objects  "obj1", "obj2", and "obj3" were pushed into ... Read More

Importance of yield() method in Java?

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 same thread will continue its execution. The advantage of yield() method is to get a chance to execute other waiting threads so if our current thread takes more time to execute and allocate processor to other threads. Syntax public static void yield() Example class MyThread extends Thread { public void run() { ... Read More

Use of 'weakMap.has()' method in JavaScript?

Manisha Patil
Updated on 23-Aug-2022 14:26:38

151 Views

The key-value pairs are stored using JavaScript WeakMap Objects. A WeakMap object differs from a Map object in that such objects should be weakly referred and "object" should be stored in the WeakMap object as a key. In comparison, you can add simple values like strings, booleans, symbols, and numbers to the Map objects. WeakMap objects are stored loosely, therefore if the references to a particular key are dropped or the object is erased, the garbage collection will drop the WeakMap element after it has verified that the value is mapped to the relevant object. The JavaScript built-in function weakMap.has() ... Read More

Advertisements