是否有Java线程中断异步队列并置于警报状态? [英] is there java thread interrupt asynchronous queue and put in alertable?

查看:105
本文介绍了是否有Java线程中断异步队列并置于警报状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像下面的链接一样,是否存在线程中断异步队列并置于警报状态的java函数?

Like below link, is there java function that thread interrupt asynchronous queue and put in alertable?

我想在Java中使用异步计时器功能.我检查了它在c ++中是否可以正常工作.但是我不知道它是否在Java中. 当主线程运行时,请定期检查是否触发了异步计时器并运行它,然后返回并再次运行.那就是我想做的. 当然,在检查异步计时器已触发时,我将使用可警报的睡眠方式.

I want to make async timer function in java. I checked it works in c++. But I don't know if it is in java. When main thread run and check periodically if there are the async timer fired and run it and going back and run again. That is what i want to do. Of course, when checking async timer fired, I will use sleep with alertable.

我试图在Google中找到它,但没有找到. 预先感谢!

I've tried to find it in google, but I didn't find. Thanks in advance!

以下是我想做的更多细节.

What I want to do more detail is below.

让我们假设有一个程序会收到类似:

Let's assume that there is a program that getting requests like :

msg1 : msgname=aa1 to=sub1 waittime=1000 msgbody 
msg2 : msgname=aa2 to=sub2 waittime=2000 msgbody 
msg3 : msgname=aa3 to=sub1 waittime=3000 msgbody 
msg4 : msgname=aa3 to=sub1 msgbody . .

,程序应将每个msg传递给msg的to1字段中所描述的sub1和sub2.

and the program should pass each msg to sub1, sub2 described in msg's to field.

如果存在waittime,它将在以后的毫秒级传递消息.程序应该在1个线程中执行此操作,并且在一秒钟内超过1万个msg.如果仅使用同步睡眠,则所有消息都不会经过一段时间并延迟.我检查它在C ++代码中是否能正常工作,而且我已经看到用Java(也许)制作的商业程序可以做到这一点.但是我是Java的新手,我想知道在Java中是可能的.

If waittime exists, it should pass the message as much as waittime millisec later. the program should do that in 1 thread, and there over 10 thousands msg in one second. If use just synchronous sleep, all msgs souldn't pass in a time and delayed. I check it works well in c++ code, and I have seen a commercial program made in java(maybe) does this. But I am novice in java and I want to know it is possible in java.

推荐答案

Java没有类似于Windows的"alertable"和APC队列的概念,而且我怀疑是否可以同时使用Windows本机API 将其与常规Java线程行为集成.

Java doesn't have concepts analogous to Windows' "alertable" and the APC queue, and I doubt that it would be possible to both use the Windows native APIs and integrate this with normal Java thread behavior.

在Java中实现计时器的简单方法是使用标准的Timer类.请参见 javadoc .如果这对您不起作用,请更详细地说明您的问题.

The simple way to implement timers in Java is to use the standard Timer class; see the javadoc. If this isn't going to work for you, please explain your problem in more detail.

针对您的后续问题:是的,在Java中是可能的.实际上.可能有很多方法可以做到.但是TimerTimerTask是一个很好的方法.像这样:

In response to your followup problem: yes it is possible in Java. In fact. there are probably many ways to do it. But Timer and TimerTask are a good a way as any. Something like this:

   public class MyTask extends TimerTask {
       private String msg;
       private String to;
       public Mytask(String msg, String to) {
          this.msg = msg;
          this.to = to;
       }

       public void run() {
           // process message
       }
   }

   Timer timer = new Timer();

   while (...) {
       // read message
       timer.schedule(new MyTask(msg, to), waitTime);
   }

这篇关于是否有Java线程中断异步队列并置于警报状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆