JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label thread interview questions. Show all posts
Showing posts with label thread interview questions. Show all posts

Sleep vs yield in Java

1:11 AM
Sleep vs yield in Java Sleep and yield are two methods which are used to get CPU back from Thread to Thread Scheduler in java but they are completely different than each other. The major difference between Sleep vs yield is that sleep is more reliable than yield and it's advised to use sleep(1) instead of yield to relinquish CPU in multi-threaded Java application to give an opportunity to other threads to execute. In this Java tutorial, we will what are differences between yield and sleep in Java. But before seeing difference between sleep and Yield let's see some similarities between yield and sleep in Java Similarities between Sleep and yield in Java  Here are some common things between sleep and yield method in Java programming : 1) Both yield and sleep are declared on java.lang.Thread class. 2) Both sleep() and yield() are static methods and operate on current thread. It doesn't matter which thread's object you used to call this method, both these meth...

Explain wait(), notify() and notifyAll() in Java Thread?

1:08 AM
If you ask me one concept in Java which is so obvious yet most misunderstood, I would say the wait(), notify() and notifyAll() methods. They are quite obvious because they are the one of the three methods of total 9 methods from java.lang.Object but if you ask when to use the wait(), notify() and notfiyAll() in Java, not many Java developer can answer with surety. The number will go down dramatically if you ask them to solve the producer-consumer problem using wait() and notify(). Many will use if block instead of while loop, many others will get confused on which object they should call wait() and notify()method? Some of them even succeed in creating livelock, deadlock, and other multithreading issues. That's why it's become very important to know as much as possible about these three methods. In this article, I am going to share some practical tips and points about wait(), notify() and notifyAll() in Java. Two books, which helped me a lot while understanding this core c...

What is What is difference between wait and sleep in Java Thread?

12:56 AM
Wait vs sleep in Java ? Differences between wait and sleep method in Java multi-threading is one of the very old questions asked in Java interviews. Though both wait and sleep put thread on waiting state, they are completely different in terms of behavior and use cases. Thread.sleep(long millis) is meant for introducing pause, releasing CPU and giving another thread an opportunity to execute while wait is used for inter thread communication in Java. These methods are defined in java.lang.Object class and available to every object in Java. It is based upon object lock, if you remember every object in Java has implicit lock, also known as monitor. When a thread enter into a synchronized method it acquired the lock which is used to protect the critical reason e.g. it acquired lock on current object if it is going inside an instance synchronized method and lock object on class literal if its entering into a static synchronized method. By using wait() and notify() method two threads can c...

Explain bout wait() and join method in Java thread

12:50 AM
Though both wait() and join() methods are used to pause the current thread and have a lot of similarities they have different purposes. One of the most obvious differences between the wait() and join() method is that former is declared in java.lang.Object class while join() is declared in java.lang.Thread class. This means that wait() is related to the monitor lock which is held by each instance of an object and join method is related to Thread itself. The wait() method is used in conjunction with notify() and notifyAll() method for inter-thread communication, but join() is used in Java multi-threading to wait until one thread finishes its execution.  Another difference between wait() and join() method is that former must be called from synchronized method or block but later can be called without a synchronized block in Java. A good knowledge of differnet thread related methods e.g. start and run, wait and notify, join and yield goes a long way in writing robuts and correct mu...

Explain Difference between synchronized block and method in Java Thread with example

12:47 AM
Synchronized block and synchronized methods are two ways to use synchronized keyword in Java and implement mutual exclusion on critical section of code. Since Java is mainly used to write multi-threading programs,  which present various kinds of thread related issues like thread-safety, deadlock and race conditions, which plagues into code mainly because of poor understanding of synchronization mechanism provided by Java programming language. Java provides inbuilt synchronized and volatile keyword to achieve synchronization in Java. Main difference between synchronized method and synchronized block is selection of lock on which critical section is locked. Synchronized method depending upon whether its a static method or non static locks on either class level lock or object lock. Class level lock is one for each class and represented by class literal e.g. Stirng.class. Object level lock is provided by current object e.g. this instance, You should never mix static and non static sy...

Explain What is Thread and Runnable in Java with Example

12:40 AM
What is Thread in Java ?  Thread in Java is an independent path of execution which is used to run two task in parallel. When two Threads run in parallel that is called multi-threading in Java. Java is multithreaded from the start and excellent support of Thread at language level e.g. java.lang.Thread class, synchronized keyword, volatile and final keyword makes writing concurrent programs easier in Java than any other programming language e.g. C++. Being multi-threaded is also a reason of Java's popularity and being number one programming language. On the other hand if your program divides a task between two threads it also brings lot of programming challenges and issues related to synchronization, deadlock, thread-safety and race conditions. In short answer of question What is Thread in Java can be given like "Thread is a class in Java but also a way to execute something in parallel independently in Java". Thread in Java requires a task which is executed by this thre...

Explain about Difference between Callable and Runnable in Java

12:33 AM
Difference between Callable and Runnable interface in Java is one of the interesting questions from my list of Top 15 Java multi-threading questions, and it’s also very popular in various Java Interviews. The Callable interface is newer than Runnable interface and added on Java 5 release along with other major changes e.g. Generics, Enum, Static imports and variable argument method. Though both Callable and Runnable interface are designed to represent a task, which can be executed by any thread, there is some significant difference between them. In my opinion, the major difference between Callable and Runnable interface is that Callable can return the result of an operation performed inside call() method, which was one of the limitations with Runnable interface. Another significant difference between Runnable and Callable interface is the ability to throw checked exception. The Callable interface can throw checked exception because it's call method throws Exception. Commonly ...

.