通过请求标头发送数据和通过请求正文发送数据有什么区别.在什么情况下,我们必须通过标题/正文发送数据,何时不应该通过标题/正文发送数据?
javascript servlets httprequest httpurlconnection http-headers
如何使用Java WebSocket API关闭websocket连接?我已经将Java websocket API用于服务器端点和客户端端点.该应用程序运行正常.但是我不知道在主线程结束之前如何关闭websocket.
这是我的ClientEndpoint
package websocket.client;
import java.io.IOException;
import javax.websocket.MessageHandler;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
@ClientEndpoint
public class EchoClient {
Session session;
//request
@OnOpen
public void onOpen(Session session, EndpointConfig config) {
System.out.println("Connected to endpoint: " + session.getBasicRemote());
this.session = session;
sendMessage("Welcome to WebSocket");
}
//response
@OnMessage
public void onMessage(String text) {
System.out.println("Received response in client from server: " + text);
}
@OnError
public void onError(Session session, Throwable t) {
t.printStackTrace();
}
private void sendMessage(String message) …Run Code Online (Sandbox Code Playgroud) 我试图了解队列中的行为ThreadPoolExecutor.在下面的程序中,当我使用时LinkedBlockingQueue,我一次只能向线程池提交一个任务.但是,如果我取代LinkedBlockingQueue用SynchronousQueue,我可以在瞬间提交所有5个任务到池中.如何SynchronousQueue不同于LinkedBlockingQueue在这种情况下?
Java程序:
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class Sample {
public static void main(String[] args) throws InterruptedException {
LinkedBlockingQueue<Runnable> threadPoolQueue = new LinkedBlockingQueue<>();
// SynchronousQueue<Runnable> threadPoolQueue = new SynchronousQueue<>();
ThreadFactory threadFactory = Executors.defaultThreadFactory();
ThreadPoolExecutor tpe = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, threadPoolQueue, threadFactory);
Runnable np;
for (int i = 1; i <= 5; i++) {
np = new SampleWorker("ThreadPoolWorker " …Run Code Online (Sandbox Code Playgroud) java multithreading blockingqueue threadpool threadpoolexecutor