100000380
Points
Questions
185
Answers
186
-
join() method will wait for the current thread to complete execution.
Let us assume that t1 and t2 are two executing threads. If t1 calls join() on t2 i.e.; t2.join() immediately t1 will enter into waiting state until t2 completes its execution.
yield() method gives a hint to the scheduler that the current thread is willing to yield its current use of a processor.
yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute. If there is no waiting thread or all the waiting threads have a lower priority then the same thread will continue its execution. The chance for execution of the yielded thread is decided by the thread scheduler whose behavior is vendor dependent.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 2091 views
- 1 answers
- 0 votes
-
Let us take the below example for the discussion. In this class, there is a static block, followed by a static method and a static variable.
public class Test { static{ System.out.println(Test.x); } public static void main(String[] args) { System.out.println("Value of x: "+Test.x); } static int x=100; }
The output of this example will be
0
Value of x: 100
In this case, first the static block is invoked because it appears first, next the static variable is initialized and the static method is invoked. The reason is that the static members are initialized in the order in which they appear in the source.
If you change the order as below:
public class Test { public static void main(String[] args) { System.out.println("Value of x: " + Test.x); } static int x = 100; static { System.out.println(Test.x); } }
The output will be
100
Value of x: 100
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1263 views
- 1 answers
- 0 votes
-
Use notify() if all your waiting threads are interchangeable (the order they wake up doesn’t matter), or if you only ever have one waiting thread. A common example is a thread pool used to execute jobs from a queue–when a job is added, one of threads is notified to wake up, execute the next job and go back to sleep.
Use notifyAll() for other cases where the waiting threads may have different purposes and should be able to run concurrently. An example is a maintenance operation on a shared resource, where multiple threads are waiting for the operation to complete before accessing the resource.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1367 views
- 1 answers
- 0 votes
-
Throwable is the super most interface in Exception hierarchy. This is inherited by Exception and Error classes.
- Error – These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example, suppose that an application successfully opens a file for input, but is unable to read the file because of a hardware or system malfunction. The unsuccessful read will throw java.io.IOError. Errors are those exceptions indicated by Error and its subclasses.
- Exception – Two kinds of Exception –
o Checked Exceptions are exceptional conditions that a well-written application should anticipate and recover from. For example, suppose an application prompts a user for an input file name, then opens the file by passing the name to the constructor for java.io.FileReader. Normally, the user provides the name of an existing, readable file, so the construction of the FileReader object succeeds, and the execution of the application proceeds normally. But sometimes the user supplies the name of a nonexistent file, and the constructor throws java.io.FileNotFoundException. All exceptions are checked exceptions, except for those indicated by Error, RuntimeException, and their subclasses..
o Unchecked Exceptions are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API. For example, consider the application described previously that passes a file name to the constructor for FileReader. If a logic error causes a null to be passed to the constructor, the constructor will throw NullPointerException. The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur. Runtime exceptions are those indicated by RuntimeException and its subclasses.
Errors and runtime exceptions are collectively known as unchecked exceptions. These classes are further inherited by a whole bunch of exception classes.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1257 views
- 1 answers
- 0 votes
-
No, Static variables cannot be elected for garbage collection. They can be collected when the respective class loader (that was responsible for loading this class) is itself collected for garbage.
Static variables are referenced by Class objects which are referenced by Class loaders – so unless the ClassLoader itself becomes eligible for collection the static variables won’t be collected.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1253 views
- 1 answers
- 0 votes
-
String s=”abc” is a String literal. Here String s refers to an interned String object. This means, that the character sequence “abc” will be stored at a central place (common pool) and whenever the same literal “abc” is used again, the JVM will not create a new String object but use the reference of the ‘cached’ String.
String s= new String (“abc”) is guaranteed to be a new String object. String objects created using ‘new’ operator are stored in the heap.
So if,
String s1="abc"; String s2="abc"; String s3 = new String("abc"); String s4 = new String("abc");
(s1 == s2) is true
s1.equals(s2) is true(s3 == s4) is false
s3.equals(s4) is trueThis answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 6323 views
- 1 answers
- 0 votes
-
If you directly call run() method its body is executed in context of current thread. When you invoke start() method a new thread is created and run() method is executed in this new thread. The run() method is just an ordinary method (overridden by you). As with any other ordinary method, calling it will cause the current thread to execute run() and not a new separate thread. The start() method will spawn a new thread and let the newly spawned thread execute run().
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 3727 views
- 1 answers
- 0 votes
-
The method System.gc() is used for running the garbage collector. Calling the gc() method suggests that the Java Virtual Machine expend effort towards recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.
The call System.gc() is effectively equivalent to the call:
Runtime.getRuntime().gc()However, you need to be very careful to call System.gc(). Calling it can add unnecessary performance issues to your application, and it is not guaranteed to actually perform a collection.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1192 views
- 1 answers
- 0 votes
-
final – final is a keyword in Java. The variable declared as final cannot be changed after initialization, the method declared as final cannot be overridden, the class declared as final cannot be inherited.
finally – finally is a block in Java which is used along with try-catch block. The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. Java 7 has a new try with resources statement that you can use to automatically close resources that explicitly or implicitly implement java.io.Closeable or java.lang.AutoCloseable
finalize – finalize is a method in java.lang.Object class. finalize() method is called by the garbage collector before reclaiming the memory allocated to the object. A class overrides the finalize() method to dispose of system resources or to perform other cleanup. However the usage of finalize() method is not recommended because the execution of finalize() method is not guaranteed and there is a severe impact on performance.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1300 views
- 1 answers
- 0 votes
-
RE: In Java, what is the difference between synchronizing a block and synchronizing a static method?
A synchronized method acquires a lock before it executes. For a static method, the lock associated with the Class object for the method’s class is used. For an instance method, the lock associated with this (the object for which the method was invoked) is used.
A synchronized block inside a method in Java is synchronized on the instance (object) owning the method. Thus, each instance has its synchronized methods synchronized on a different object: the owning instance. Only one thread can execute inside a Java code block synchronized on the same monitor object.
Synchronized static methods are synchronized on the class object of the class the synchronized static method belongs to. Since only one class object exists in the Java VM per class, only one thread can execute inside a static synchronized method in the same class.
This answer accepted by JavaNinja. on 18th September 2018 Earned 15 points.
- 1140 views
- 1 answers
- 0 votes