Morgan Stanley interview--java Multithreading

Source: Internet
Author: User

This morning, take part in the interview of the Morgan Stanley, consciously failed, record the process.

The interviewer looked at the resume and did not ask questions about the resume, and the whole process was asking the Java Multi-threading question.

1. Reentrantlock, as a reentrant lock, how to understand the "reentrant" two words, there is no non-repeatable lock?

Me: The thread that gets the lock on the object can get the object lock again, access the object ... Was despised, and later thought, should be recursive to this scenario, say;

2. How is the producer-consumer model implemented?

Me: Using synchronized or lock for these synchronization methods.

The interviewer asked, why not use some more advanced packaging?

Me: You can use the Exchanger class.

Interviewer: You can use Blockingqueue, then,

3. As an interface, what are the specific implementation classes? How to implement a blockingqueue, please implement it?

I: Not clear concrete implementation (later checked, there are arrayblockingqueue,linkedblocking,priorityblockingqueue,delayqueue,synchronousqueue, the first two most common).

You can use list to store data, use lock and condition to ensure synchronization, the code is as follows ( preferably using a template ),

public class Definedblockingqueue {private linkedlist<integer> queue;private Lock lock;private int max;private Condition empty;private Condition full;public definedblockingqueue (linkedlist<integer> queue, int max) { This.queue = Queue;this.max = Max;lock = new Reentrantlock (); full = Lock.newcondition (); empty = Lock.newcondition ();} Public integer Take () {Lock.lock (), Integer t = null;try {while (Queue.isempty ()) {full.await ();} t = Queue.poll (); Empty.signalall (); return t;} catch (Interruptedexception e) {//E should be processed} finally {Lock.unlock ();} return t;} public void put (Integer e) {lock.lock (), try {while (queue.size () = max) {empty.await ();} Queue.add (e); Full.signalall ();} catch (Interruptedexception e) {//E should be processed} finally {Lock.unlock ();}}}

4. Why use condition and lock instead of synchronized and wait () to implement Blockingqueue ()?

I: The former has better characteristics, such as trylock, reading and writing locks.

Later I looked up the information and added:

the lock interface supports a more flexible synchronization code block structure: usingsynchronized a keyword, you can only get and release control in the same synchronized block structure. The lock interface allows for more complex critical section structures (control acquisition and release does not appear in the same block structure), such as the Arrayblockingqueue class
void removeAt (int i) {        final object[] items = this.items;        If removing front item, just advance        if (i = = Takeindex) {            Items[takeindex] = null;            Takeindex = Inc (Takeindex);        } else {            //slide-others up through Putindex.            for (;;) {                int nexti = Inc (i);                if (Nexti! = putindex) {                    items[i] = Items[nexti];                    i = Nexti;                } else {                    items[i] = null;                    Putindex = i;                    Break        ;        }}} --count;        Notfull.signal ();    }

Morgan Stanley interview--java Multithreading

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.