Raja has Published 760 Articles

Importance of wait(), notify() and notifyAll() methods in Java?

raja

raja

Updated on 27-Nov-2023 11:04:59

13K+ Views

The threads can communicate with each other through wait(), notify() and notifyAll() methods in Java. These are final methods defined in the Object class and can be called only from within a synchronized context. The wait() method causes the current thread to wait until another thread invokes the notify() or ... Read More

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

raja

raja

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

404 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

Importance of join() method in Java?

raja

raja

Updated on 27-Nov-2023 09:31:03

3K+ Views

A join() is a final method of 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 ... Read More

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

raja

raja

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

4K+ 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

How to check the memory used by a program in Java?

raja

raja

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

1K+ Views

For a long-running java code, which makes heavy use of dynamic memory, we may end up with Out-Of-Memory errors due to a memory shortage of the heap space. In the below program, we can test the free java heap space used by a program. If the heap space is used more ... Read More

How to print the maximum occurred character of a string in Java?

raja

raja

Updated on 24-Nov-2023 11:20:31

353 Views

A String class can be used to represent the character strings, all the string literals in Java programs are implemented as an instance of the String Class. The Strings are constants and their values cannot be changed (immutable) once created. In the below program, we can print the maximum occurred character ... Read More

How can we print all the capital letters of a given string in Java?

raja

raja

Updated on 24-Nov-2023 11:17:32

4K+ Views

The Character class is a subclass of Object class and it wraps a value of the primitive type char in an object. An object of type Character class contains a single field whose type is char. We can print all the uppercase letters by iterating the characters of a string in ... Read More

When to call the Thread.run() instead of Thread.start() in Java?

raja

raja

Updated on 24-Nov-2023 10:50:04

2K+ Views

When we call the start() method on a thread it causes the thread to begin execution and run() method of a thread is called by the Java Virtual Machine(JVM). If we call directly the run() method, it will be treated as a normal overridden method of a thread class (or runnable ... Read More

How can we sort a string without using predefined methods in Java?

raja

raja

Updated on 24-Nov-2023 10:44:38

4K+ Views

A String is an object that represents an immutable sequence of characters and cannot be changed once created. The java.lang.String class can be used to create a string object. In the below program, we can sort the characters of a string without using any predefined methods of String class in Java. Example public ... Read More

Can we define multiple methods in a class with the same name in Java?

raja

raja

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

7K+ Views

Yes, we can define multiple methods in a class with the same name but with different types of parameters. Which method is to get invoked will depend upon the parameters passed. In the below example, we have defined three display methods with the same name but with different parameters. Depending on ... Read More

Advertisements