对阻塞、非阻塞,同步、异步初步了解了,不是太明白,其中大多数将的是对于CPU的使用率及对一个事件的处理效率。 阻塞与非阻塞各有优缺点,在知乎上也看到了下面的结论:
在处理 IO 的时候,阻塞和非阻塞都是同步 IO。 只有使用了特殊的 API 才是异步 IO。
一、Tyrus发送消息的两种方式:
The interface javax.websocket.RemoteEndpoint, part of Java API for WebSocket, is designed to represent the other end of the communication (related to the endpoint), so the developer uses it to send the message. There are two basic interfaces the user may use - javax.websocket.RemoteEndpoint$Basic andjavax.websocket.RemoteEndpoint$Async.
javax.websocket.RemoteEndpoint接口,是JAVA API中的WebSocket的一部分,它被设计为与连接的另一端进行通信,开发人员可以使用它发送消息。有两种基本的接口可以使用:接口javax.websocket.RemoteEndpoint$Basic(同步消息)和javax.websocket.RemoteEndpoint$Async(异步消息)
1:同步消息传递(javax.websocket.RemoteEndpoint$Basic)
is used to send synchronous messages The point of completion of the send is defined when all the supplied data has been written to the underlying connection. The methods for sending messages on the javax.websocket.RemoteEndpoint$Basic block until this point of completion is reached, except forjavax.websocket.RemoteEndpoint$Basic#getSendStream() and javax.websocket.RemoteEndpoint$Basic#getSendWriter() which present traditional blocking I/O streams to write messages. See the example "Sending message via RemoteEndpoint.Basic instance" to see how the whole text message is send. The following example demonstrates a method which sends the partial text method to the peer:
javax.websocket.RemoteEndpoint$Basic接口被用来发送同步消息,当所有提供的数据都被写入到底层连接时,完成发送的标识被确定。通过javax.websocket.RemoteEndpoint$Basic接口发送消息的方法会被阻塞,直到完成发送的标识到达。javax.websocket.RemoteEndpoint$Basic#getSendStream()方法和javax.websocket.RemoteEndpoint$Basic#getSendWriter()方法 通过传统的传统的阻塞I / O流写入消息。
这里的完成发送的标识应该是方法中的isLast参数(Boolean),这里的了解的还不深入,之前一直在找WebSocket客户端处理消息的方式是什么,通过自己的几个小测试应该是多线程的。