Java POJO:处理服务器请求对象队列的策略

发布于 2024-11-01 07:39:03 字数 1817 浏览 3 评论 0原文

现在,我正在决定处理发送到服务器的请求对象的最佳方式。换句话说,我在我的应用程序中具有跟踪请求对象,例如印象和点击跟踪。负载非常低的简单请求。在我的应用程序中的某些地方,需要跟踪的所述对象同时出现在彼此旁边(我必须跟踪最多三个并发对象),因此每次所述对象可见时,我都必须创建一个跟踪请求他们每个人的对象。

现在我已经知道我可以轻松创建一个单例队列线程,它将这些对象添加到向量中,并且我的线程要么在主循环中处理它们,要么在队列上调用等待,直到我们有对象要处理。虽然这听起来像是一个明确的解决方案,但队列可能会累积到数十个,这有时会很麻烦,因为它为每个请求建立一个连接,因此它不会同时运行。

我的想法是创建一个线程池,它允许我通过信号量创建两个并发连接,并处理包含跟踪事件请求的线程对象。换句话说,我想创建一个函数来创建一个新的线程对象并将其添加到 Vector 中,其中线程池将迭代线程集并一次处理两个线程。我知道我可以创建一个函数来添加对象,如下所示:

public boolean addThread(Runnable r){
synchronized(_queue){
    while(!dead){
       _queue.addElement(r);
       //TODO: How would I notify my thread pool object to iterate through the list to process the queue? Do I call notify on the queue object, but that would only work on a thread right??
       return true
    }
    return false;
}

我想知道线程本身将如何执行。如何编写一个在将线程添加到列表后执行线程池的函数?另外,由于信号量将在第二次连接后阻塞,因此会锁定我的应用程序直到有一个开放插槽,还是会在循环列表时锁定在线程池对象中?

与往常一样,由于我的目标是 J2ME/Blackberry 环境,因此仅接受 1.5 之前的答案,因此 Concurrent 包中没有泛型或任何类。

编辑:所以我认为这或多或少应该是这样的:

class MyThreadPool extends Thread{

  private final Vector _queue = new Vector();
  private CappedSemaphore _sem;
  public MyWaitingThread (){
      _sem = new CappedSemaphore(2);
      this.start();
  }
  public void run(){
     while(!dead){
        Runnable r = null;
        synchronized(_queue){
          if(_queue.isEmpty()){
            _queue.wait();
          } else {
            r = _queue.elementAt(0);
            _queue.removeElement(0);
          }
       }
       if(r != null){
          _sem.take();
          r.run();
          _sem.release();
       }
    }
 }
 public boolean addThread(Runnable r){
   synchronized(_queue){
   if(!dead){
     _queue.addElement(r);
     _queue.notifyAll();
     return true
   }
   return false;
 }
}

Right now I'm torn up in deciding the best way of handling request objects that I send up to a server. In other words, I have tracking request objects for things such as impression and click tracking within my app. Simple requests with very low payloads. There are places in my app where said objects that need to be tracked appear concurrently next to each other (at most three concurrent objects that I have to track), so every time said objects are visible for example, I have to create a tracking request object for each of them.

Now I already know that I can easily create a singleton queue thread which adds those objects into a vector and my thread either processes them in the main loop or calls wait on the queue until we have objects to process. While this sounds like a clear cut solution, the queue can accumulate into the dozens, which can be cumbersome at times, since it's making one connection for each request, thus it won't run concurrently.

What I had in mind was to create a thread pool which would allow me to create up two concurrent connections via semaphore and process thread objects that would contain my tracking event requests. In other words, I wanted to create a function that would create a new thread Object and add it into a Vector, in which the thread pool would iterate through the set of threads and process them two at a time. I know I can create a function that would add objects like so:

public boolean addThread(Runnable r){
synchronized(_queue){
    while(!dead){
       _queue.addElement(r);
       //TODO: How would I notify my thread pool object to iterate through the list to process the queue? Do I call notify on the queue object, but that would only work on a thread right??
       return true
    }
    return false;
}

What I am wondering is how will the threads themselves get executed. How can I write a function that would execute the thread pool after adding a thread to the list? Also, since the semaphore will block after the second connection, will that lock up my app until there is an open slot, or will it just lock up in the thread pool object while looping through the list?

As always, since I am targeting a J2ME/Blackberry environment, only pre-1.5 answers will be accepted, so no Generics or any class from the Concurrent package.

EDIT: So I take it that this is what it should look like more or less:

class MyThreadPool extends Thread{

  private final Vector _queue = new Vector();
  private CappedSemaphore _sem;
  public MyWaitingThread (){
      _sem = new CappedSemaphore(2);
      this.start();
  }
  public void run(){
     while(!dead){
        Runnable r = null;
        synchronized(_queue){
          if(_queue.isEmpty()){
            _queue.wait();
          } else {
            r = _queue.elementAt(0);
            _queue.removeElement(0);
          }
       }
       if(r != null){
          _sem.take();
          r.run();
          _sem.release();
       }
    }
 }
 public boolean addThread(Runnable r){
   synchronized(_queue){
   if(!dead){
     _queue.addElement(r);
     _queue.notifyAll();
     return true
   }
   return false;
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2024-11-08 07:39:03

您想要做的,是在线程侧让每个线程在队列中等待。例如

class MyWaitingThread extends Thread{

   private final Queue _queue;
   public MyWaitingThread (Queue _queue){
      this._queue = _queue;
   }
   public void run(){
      while(true){
       Runnable r = null;
       synchronized(_queue){
            if(_queue.isEmpty())
                _queue.wait();
            else
               r = queue.pop();
        }
      if(r != null) r.run();
      }
   }
}

,在您的其他逻辑中,它看起来像:

public void addThread(Runnable r){
     if(!dead){
       synchronized(_queue){
         _queue.addElement(r);
         _queue.notifyAll();
       }
     }
}

_queue.notifyAll 将唤醒在 _queue 实例上等待的所有线程。另外,请注意,我将 while(!dead) 移到了同步块之外,并将其更改为 if(!dead)。我可以想象保持它原来的方式它不会像你希望的那样工作。

What you would want to do, in on the thread side have the each thread wait on the queue. For example

class MyWaitingThread extends Thread{

   private final Queue _queue;
   public MyWaitingThread (Queue _queue){
      this._queue = _queue;
   }
   public void run(){
      while(true){
       Runnable r = null;
       synchronized(_queue){
            if(_queue.isEmpty())
                _queue.wait();
            else
               r = queue.pop();
        }
      if(r != null) r.run();
      }
   }
}

And in your other logic it would look like:

public void addThread(Runnable r){
     if(!dead){
       synchronized(_queue){
         _queue.addElement(r);
         _queue.notifyAll();
       }
     }
}

That _queue.notifyAll will wake up all threads waiting on the _queue instance. Also, notice I moved the while(!dead) outside of the synchronized block and changed it to if(!dead). I can imagine keeping it the way you originally had it wouldnt have worked exactly like you hoped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文