高分辨率定时器在java [英] high resolution timer in java

查看:111
本文介绍了高分辨率定时器在java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中模拟TCP。



因为我有多个线程,例如每个TCP连接的发送者和接收者线程。



我的问题是,我想暂停(像Thread.sleep())线程微秒时间间隔。所以我可以模拟流控制,发送器线程将在发送下一个数据包之前阻塞微秒,同时CPU可以通过接收和数据处理线程使用。但我找不到任何方法执行sleep()或wait()微秒或纳秒的分辨率。



我发现System.nanoTime()方法,但是没有方法阻止线程的指定micro或纳秒。如果有任何这样的方法,那么请让我知道。 System.nanoTime()只是以纳秒为单位给出相对时间间隔。



我可以使用System.nanoTime()在使用busy-loop的线程中执行纳秒延迟,



另一个令人困惑的问题:



通过浏览互联网,我发现在Windows系统中,Thread.sleep()或wait()方法块至少指定毫秒或多个10毫秒,以较少者为准,无线程中断。但是当我运行示例示例时,我发现非常不同的结果。相似的线程睡眠时间小于指定的毫秒数。有些线程给我100微秒的睡眠。是时间的误差测量吗?是不是System.nanoTime()更准确吗?见下面的例子,我得到非常不同的结果。线程优先级也没有给出预测结果。

  public class MyClass {
public static void main(String args [] ){
Runnable r = new Runnable(){
public void run(){
long s = System.nanoTime();
//Thread.yield(); //尝试yield,需要检查 - 高优先级线程在产生
后将被调度多少时间try {
Thread.sleep(1);
} catch(Exception e){

}
long e = System.nanoTime() - s;
System.out.println(thread.currentThread()。getName()+ e);
}
};

线程t1 =新线程(r,线程T1:);
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();

线程t2 =新线程(r,线程T2:);
t2.setPriority(Thread.MIN_PRIORITY);
t2.start();

线程t3 =新线程(r,Thread T3:);
t3.setPriority(Thread.NORM_PRIORITY);
t3.start();

线程t4 =新线程(r,Thread T4:);
t4.setPriority(Thread.MAX_PRIORITY);
t4.start();

long s = System.nanoTime();
try {
Thread.sleep(1);
} catch(Exception e){

}
long e = System.nanoTime() - s;
System.out.println(Main:+ e);
}
}



在我的系统中使用Intel Core i5,2.53 GHz,我得到类似输出类似下面在多个运行。 *下面的输出中的新行不打印在这篇文章中,我已经从控制台复制输出,但它给我的新行在我的控制台,这没有问题。



线程T1:736682
线程T3:700212
线程T2:478559
主线:548257
线程T4:458299



第二次 -



主题T3:27555
主题T2:139799
主题T1:152361
主要:93199
Thread T4:29986



第三次 -



主题T4:724525
主题T2:1050319
Main:752486
线程T1:941316
线程T3:1000883



第四次运行 -



线程T1:174243
线程T2:166543
线程T3:1005745
主线:1023170
线程T4:915787



在上面的运行中,我得到微秒阻塞。 :O



请指导我。



感谢您阅读整篇文章。

解决方案

您在操作系统线程调度程序的怜悯,你不能关于它。只是因为你睡了很短的时间并不意味着调度程序会在你的线程刚刚完成睡眠时唤醒。线程优先级只是一个提示,没有任何保证。


I want to simulate TCP in Java.

For that I have multiple threads, like sender and receiver threads for every TCP connection.

My problem is, I want to pause (like Thread.sleep()) threads for microseconds time interval. So that I can simulate flow control, where sender thread will block for microseconds before sending next packet, and at same time CPU can be used by receiving and data processing threads. But I couldn't find any methods which perform sleep() or wait() for microsecond or nanosecond resolution. How can I block (pause) threads in Java with micro or nanosecond resolution?

I found System.nanoTime() method, but there no methods for blocking threads for specified micro or nano seconds. If there any such methods, then please let me know. System.nanoTime() just gives relative time interval in nanoseconds.

I can use System.nanoTime() to perform nanosecond delay in threads using busy-loop, but that will waste CPU, which could have been used for receiving data thread or processing thread.

Another confusing question:

By surfing internet, I found Thread.sleep() or wait() methods blocks for at least specified milliseconds or in multiple of 10ms, whichever is less, without thread interrupt, in Windows System. But when I run sample examples, I found very different results. Like threads are sleeping less than specified milliseconds. Some threads are giving me 100 microsecond sleep. Is it error in time measured? Is not System.nanoTime() that much accurate? See below example where I am getting very different results. Thread priorities are also not giving predicted results.

public class MyClass {
    public static void main(String args []){
        Runnable r = new Runnable() {
            public void run() {
                long s = System.nanoTime();
                //Thread.yield();  // Try for yield, need to check - in how much time high priority thread will be scheduled back after yielding
                try{
                    Thread.sleep(1);
                }catch(Exception e){

                }
                long e = System.nanoTime() - s;
                System.out.println(Thread.currentThread().getName()+e);
            }
        };

        Thread t1 = new Thread(r, "Thread T1: ");
        t1.setPriority(Thread.MAX_PRIORITY);
        t1.start();

        Thread t2 = new Thread(r, "Thread T2: ");
        t2.setPriority(Thread.MIN_PRIORITY);
        t2.start();

        Thread t3 = new Thread(r, "Thread T3: ");
        t3.setPriority(Thread.NORM_PRIORITY);
        t3.start();

        Thread t4 = new Thread(r, "Thread T4: ");
        t4.setPriority(Thread.MAX_PRIORITY);
        t4.start();

        long s = System.nanoTime();
        try{
            Thread.sleep(1);
        }catch(Exception e){

        }
        long e = System.nanoTime() - s;
        System.out.println("Main: "+e);
    }
}

In my System with Intel Core i5, 2.53 GHz, I am getting outputs similar like below in multiple runs. *New-line in below output is not printed in this post, I have copied output from console, but its giving me new-line in my my console, that's not problem.

Thread T1: 736682 Thread T3: 700212 Thread T2: 478559 Main: 548257 Thread T4: 458299

Second run -

Thread T3: 27555 Thread T2: 139799 Thread T1: 152361 Main: 93199 Thread T4: 29986

Third run -

Thread T4: 724525 Thread T2: 1050319 Main: 752486 Thread T1: 941316 Thread T3: 1000883

Fourth run -

Thread T1: 174243 Thread T2: 166543 Thread T3: 1005745 Main: 1023170 Thread T4: 915787

In above runs, I am getting microseconds blocking. :O

Please guide me on this.

Apologize for long post. Thank you for reading whole post.

解决方案

You're at the mercy of the OS thread scheduler, and you can't do much about it. Just because you sleep for a very short time doesn't mean the scheduler will awake your thread as soon as it has finished sleeping. And thread priorities are only a hint, without any guarantee.

这篇关于高分辨率定时器在java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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