Java Multithreading and Concurrency basics interview questions and Answers

Source: Internet
Author: User
Tags volatile

Wait (), notify (), and Notifyall () are methods in the object class:

12345678910111213141516171819202122232425 /** * Wakes up a single thread that is waiting on this object‘s * monitor. If any threads are waiting on this object, one of them * is chosen to be awakened. The choice is arbitrary and occurs at * the discretion of the implementation. A thread waits on an object‘s * monitor by calling one of the wait methods */publicfinalnativevoidnotify();/** * Wakes up all threads that are waiting on this object‘s monitor. A * thread waits on an object‘s monitor by calling one of the * wait methods. */publicfinalnativevoidnotifyAll();/** * Causes the current thread to wait until either another thread invokes the * {@link java.lang.Object#notify()} method or the * {@link java.lang.Object#notifyAll()} method for this object, or a * specified amount of time has elapsed. * <p> * The current thread must own this object‘s monitor. */publicfinalnativevoid wait(longtimeout) throwsInterruptedException;

The following information can be found from the text descriptions of these three methods:

1) The Wait (), notify (), and Notifyall () methods are local methods and are final methods and cannot be overridden.

2) The Wait () method that invokes an object can cause the current thread to block, and the current thread must have a monitor (that is, a lock) on this object

3) Invoking an object's Notify () method wakes up a thread waiting for the object's monitor, and if more than one thread waits for the object's monitor, only one of the threads can be awakened;

4) Call the Notifyall () method to wake up all the threads that are waiting for this object's monitor;

1. What is the difference between a process and a thread?

A process is a standalone (self contained) operating environment that can be viewed as a program or an application. A thread is a task that executes in a process. The Java Runtime environment is a single process that contains different classes and programs. Threads can be called lightweight processes. Threads require fewer resources to create and reside in a process, and can share resources in a process.

2. What are the benefits of multithreaded programming?

In multithreaded programs, multiple threads are executed concurrently to improve the efficiency of the program, and the CPU does not go idle because a thread needs to wait for resources. Multiple threads share heap memory, so creating multiple threads to perform some tasks is better than creating multiple processes. For example, Servlets is better than CGI because Servlets supports multithreading and CGI does not support it.

How does the volatile keyword work in Java?

When we use the volatile keyword to modify a variable, the thread will read the variable directly and not cache it. This ensures that the variables read by the thread are consistent with the memory.

Java Multithreading and Concurrency basics interview questions and Answers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.