繁体   English   中英

Java中线程的同步

[英]Synchronization in threads for Java

我的应用程序中有一个自制的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);
  }
}

线程代码

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);
  }
}

您可以使用java.util.concurrent.CountDownLatch ,计数为1。 安排由父线程和子线程创建和共享的实例(例如,在HttpRequest的构造函数中创建它,并使其可由成员函数检索)。 然后服务器在其上调用await() ,当线程准备释放其父节点时,线程会点击countDown()

您可能需要使用Java Condition 来自文档:

条件(也称为条件队列或条件变量)为一个线程提供暂停执行(“等待”)的手段,直到另一个线程通知某个状态条件现在可能为真。

首先,我赞同其他人的观点,即在这里重新发明轮子很可能会为你带来各种各样的问题。 但是,如果你想沿着这条路走下去,那么你要做的事情并不困难。 您是否尝试过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;
  }
}

在调试器下运行并设置断点?

如果不可行,那么从System.in读取一行?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM