编程知识 cdmana.com

I admire my freshman's understanding of Java's condition interface, so I offer her face to face!

On the day , I'm still reading and studying peacefully , Xuemei came to me all of a sudden , Asked about : Good students , Do you understand Condition Interface ? Can you teach me ?

See Xuemei coming , I was in spirits right away , Speaking of :

Condition That is to realize the condition variables in the tube side .
Java There is only one condition variable in the built-in program of the language , and Lock&Condition The implemented pipeline supports multiple condition variables .
Because multiple conditional variables are supported , Make the code more readable , It's also easier to implement .
for example , You see, I've implemented a blocking queue here , You need two conditional variables .

Lovely schoolgirl , I sincerely asked : So how to use two conditional variables to implement blocking queue ?

A blocking queue , You need two conditional variables :

  • The queue is not empty ( You can't get out of an empty queue )
  • The line is not satisfied ( The queue is full )

Lock and Condition The realized pipeline , Thread waiting and notification need to call await()、signal()、signalAll(), Their semantics and wait()、notify()、notifyAll() identical .

  • Lock&Condition Only... Can be used in the implemented pipeline await()、signal()、signalAll()
  • synchronized Only the realized tube can be used wait()、notify()、notifyAll()

If in Lock&Condition Call... In the implemented pipe procedure wait()、notify()、notifyAll(), You're one step closer to leaving .

JDK Of Lock and Condition But it's an implementation of the tube process , Look at the Dubbo in ,Lock and Condition How does it work .

We need to know , What is synchronous and asynchronous ?

  • Sync
    The caller needs to wait for the result
  • asynchronous
    There's no need to wait for the results

How to implement asynchrony in the code ?

  • The caller creates a child thread , Perform method calls in child threads , That is, asynchronous call
  • Method implementation time , Create a new thread to execute the main logic , The main thread directly return, Asynchronous method .

There are a lot of asynchronous scenarios , such as TCP The protocol itself is asynchronous , Everyday RPC call , stay TCP Agreement level , Finished sending RPC After the request , Threads don't wait RPC In response to the results .

Are you curious , For everyday use RPC All calls are synchronous ? Is this synchronous or asynchronous ?

It's easy to imagine , Someone must have helped to make asynchronous to synchronous . such as RPC frame Dubbo, How does it work ?

For the following simple RPC call , By default sayHello() It's a synchronization method , The perform service.sayHello(“dubbo”) when , The thread will stop and wait for the result .

DemoService service =  The initialization section is omitted 
String message = service.sayHello("dubbo");
System.out.println(message);
  • If at this time dump The call stack of the calling thread

    Found calling thread blocked , The thread state is TIMED_WAITING. Originally, sending requests is asynchronous , But the calling thread is blocked , explain Dubbo Help us do the asynchronous to synchronous thing . Through the call stack, we can see that the thread is blocked in DefaultFuture.get(), therefore Dubbo The function of asynchronous to synchronous should be through DefaultFuture Realization .

DefaultFuture.get() What happened before :

Our expectations :

  • RPC Before returning the results , Block calling thread , Let the calling thread wait
  • RPC After returning the result , Wake up calling thread , Let the calling thread execute again

This is the classic wait - A notification mechanism . Also thought of the realization scheme of tube side . Look at the ancient version of Dubbo How did it happen .

Okay , Class is over !

版权声明
本文为[The official account -JavaEdge]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/04/20210421231754995x.html

Scroll to Top