Java 线程中的同步
我的应用程序中有一个自制的网络服务器。该 Web 服务器为每个进入套接字并被接受的请求生成一个新线程。我希望 Web 服务器等待,直到在它刚刚创建的线程中达到特定点。
我已经浏览过该网站上的许多帖子和网络上的示例,但在我告诉线程等待后,无法让网络服务器继续进行。一个基本的代码示例就很好了。
同步关键字是解决这个问题的正确方法吗?如果是这样,如何实现这一目标?我的应用程序的代码示例如下:
Web 服务器
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);
}
}
线程代码
public void run() {
//code here
//notify web server to continue here
}
更新 - 最终代码如下。每当我发送响应标头时,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);
}
}
I have a home grown web server in my app. This web server spawns a new thread for every request that comes into the socket to be accepted. I want the web server to wait until a specific point is hit in the thread it just created.
I have been through many posts on this site and examples on the web, but cant get the web server to proceed after I tell the thread to wait. A basic code example would be great.
Is the synchronized keyword the correct way to go about this? If so, how can this be achieved? Code examples are below of my app:
Web Server
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);
}
}
Thread code
public void run() {
//code here
//notify web server to continue here
}
Update - Final code is as below. The HttpRequest
does just call resumeListener.resume()
whenever I send a response header (of course also adding the interface as a separate class and the addResumeListener(ResumeListener r1)
method in HttpRequest
):
Web Server portion
// 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 java.util.concurrent.CountDownLatch< /a> 计数为 1。安排它的实例由父线程和子线程创建和共享(例如,在 HttpRequest 的构造函数中创建它,并使其可由成员函数检索)。然后,服务器对其调用
await()
,线程在准备好释放其父级时调用countDown()
。You can use java.util.concurrent.CountDownLatch with a count of 1 for this. Arrange for an instance of it to be created and shared by the parent and child thread (for example, create it in
HttpRequest
's constructor, and have it retrievable by a member function). The server then callsawait()
on it, and the thread hitscountDown()
when it's ready to release its parent.您可能需要使用 Java 条件。来自文档:
You probably need to use a Java Condition. From the docs:
首先,我同意其他人的观点,即在这里重新发明轮子很可能会给您带来各种问题。但是,如果您无论如何都想走这条路,那么您所做的事情并不困难。你尝试过 Jetty 吗?
也许是这样的:
First of all, I echo the sentiment of others that re-inventing the wheel here will most likely lead to a variety of issues for you. However, if you want to go down this road anyway what you are trying to do is not difficult. Have you experimented with Jetty?
Maybe something like this:
在调试器下运行并设置断点?
如果不可行,则从 System.in 中读取一行?
Run under a debugger and set a breakpoint?
If unfeasible, then read a line from System.in?