Ken*_*Ken 5 java multithreading synchronized
我的应用程序中有一个自制的Web服务器.此Web服务器为进入要接受的套接字的每个请求生成一个新线程.我希望Web服务器等到它刚创建的线程中的特定点被命中.
我已经浏览过本网站上的很多帖子和网络上的例子,但在告诉线程等待之后,我无法继续使用Web服务器.一个基本的代码示例会很棒.
synchronized关键字是正确的方法吗?如果是这样,怎么能实现呢?代码示例在我的应用程序下面:
网络服务器
while (true) {
//block here until a connection request is made
socket = server_socket.accept();
try {
//create a new HTTPRequest object for every file request
HttpRequest request = new HttpRequest(socket, this);
//create a new thread for each request
Thread thread = new Thread(request);
//run the thread and have it return after complete
thread.run();
///////////////////////////////
wait here until notifed to proceed
///////////////////////////////
} catch (Exception e) {
e.printStackTrace(logFile);
}
}
Run Code Online (Sandbox Code Playgroud)
线程代码
public void run() {
//code here
//notify web server to continue here
}
Run Code Online (Sandbox Code Playgroud)
更新 - 最终代码如下.的HttpRequest不只是调用resumeListener.resume()每当我发送响应报头(当然也加入该接口作为一个单独的类和的addResumeListener(ResumeListener r1)中方法HttpRequest):
Web服务器部分
// server infinite loop
while (true) {
//block here until a connection request is made
socket = server_socket.accept();
try {
final Object locker = new Object();
//create a new HTTPRequest object for every file request
HttpRequest request = new HttpRequest(socket, this);
request.addResumeListener(new ResumeListener() {
public void resume() {
//get control of the lock and release the server
synchronized(locker) {
locker.notify();
}
}
});
synchronized(locker) {
//create a new thread for each request
Thread thread = new Thread(request);
//run the thread and have it return after complete
thread.start();
//tell this thread to wait until HttpRequest releases
//the server
locker.wait();
}
} catch (Exception e) {
e.printStackTrace(Session.logFile);
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用java.util.concurrent.CountDownLatch,计数为1.安排由父线程和子线程创建和共享它的实例(例如,在HttpRequest构造函数中创建它,并使其可由成员函数检索).然后服务器调用await()它,线程countDown()在准备释放其父节点时命中.
首先,我同意其他人的观点,即在这里重新发明轮子很可能会给您带来各种问题。然而,如果你无论如何都想走这条路,那么你所做的事情并不困难。你尝试过 Jetty 吗?
也许是这样的:
public class MyWebServer {
public void foo() throws IOException {
while (true) {
//block here until a connection request is made
ServerSocket socket = new ServerSocket();
try {
final Object locker = new Object();
//create a new HTTPRequest object for every file request
MyRequest request = new MyRequest(socket);
request.addResumeListener(new ResumeListener() {
public void resume() {
locker.notify();
}
});
synchronized(locker){
//create a new thread for each request
Thread thread = new Thread(request);
//start() the thread - not run()
thread.start();
//this thread will block until the MyRequest run method calls resume
locker.wait();
}
} catch (Exception e) {
}
}
}
}
public interface ResumeListener {
public void resume();
}
public class MyRequest implements Runnable{
private ResumeListener resumeListener;
public MyRequest(ServerSocket socket) {
}
public void run() {
// do something
resumeListener.resume(); //notify server to continue accepting next request
}
public void addResumeListener(ResumeListener rl) {
this.resumeListener = rl;
}
}
Run Code Online (Sandbox Code Playgroud)