Thread.sleep(1)为啥不起作用?请高手帮忙分析!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thread.sleep(1)为啥不起作用?请高手帮忙分析!相关的知识,希望对你有一定的参考价值。

如下测试并发代码,在Callme类里面产生毫秒级的流水号System.currentTimeMillis(),为了禁止重复,在同步方法里面睡眠1毫秒,保证下个线程进入时已经过了1毫秒,System.currentTimeMillis()值不会和上一个线程的重复。
但运行结果发现还会产生重复的流水号,如果sleep时间调到500毫秒或者再长点,就不会重复了,sleep(10)的时候也会重复,请各位高手指点迷津。是不是java的thread有什么机制?
class Callme
public synchronized static void call(String msg)
System.out.print("[" + msg +":");
System.out.print(System.currentTimeMillis());
System.out.println("]");
try
Thread.sleep(1);
catch (InterruptedException e)
e.printStackTrace();




class Caller extends Thread
public void run()
Callme.call("hello");



public class Test_sleep
public static void main(String[] args)
for(int i = 0; i < 999; i++)
Caller obj = new Caller();
obj.start();



运行结果:
[hello:1316771482331]
[hello:1316771482331]
[hello:1316771482331]
[hello:1316771482331]
[hello:1316771482331]
[hello:1316771482331]
[hello:1316771482331]
[hello:1316771482331]
[hello:1316771482346]
[hello:1316771482346]
[hello:1316771482346]
[hello:1316771482346]
[hello:1316771482346]
[hello:1316771482346]
[hello:1316771482346]
[hello:1316771482346]
[hello:1316771482362]
[hello:1316771482362]
[hello:1316771482362]
[hello:1316771482362]
[hello:1316771482362]
[hello:1316771482362]
[hello:1316771482362]
[hello:1316771482362]
[hello:1316771482378]
[hello:1316771482378]
[hello:1316771482378]
[hello:1316771482378]
[hello:1316771482378]
[hello:1316771482378]
[hello:1316771482378]
[hello:1316771482378]
[hello:1316771482393]
[hello:1316771482393]
[hello:1316771482393]
[hello:1316771482393]
[hello:1316771482393]
[hello:1316771482393]
[hello:1316771482393]
[hello:1316771482393]

不是Thread.Sleep(1)没起作用,而是System.currentTimeMillis()的精度不够。
根据javadoc的说明,System.currentTimeMillis()虽然返回以毫秒计数的时间,但是能不能精确到一毫秒是要看操作系统的计数精度的。而大多数操作系统都不能以1毫秒单位做计数。
For example, many operating systems measure time in units of tens of milliseconds.
比如,mac或者Linux计数精度可能接近1毫秒,但是windows可能是50毫秒(具体数值请另查)。
看你上面给的结果,你用的操作系统好像精度在15-16毫秒。

如果你用的是jdk1.5以上,并且只是取流水号而不是精确计时的话,可以用System.nanoTime()代替。追问

看了一下jdk api,是这样的,谢谢!

参考技术A 1毫秒在cpu那么高的频率面前根本就微乎其微,可以忽略的

为啥background 不起作用

background:#fefefe;
background-image: url(/templates/red/html/images/main.jpg);
background-position:center top;
background-repeat:no-repeat;
font-size:14px;
如上我定义的body样式,为什么background:#fefefe; 这句不起作用,背景尺寸不能布满全屏,空余部分我要用颜色,为什么颜色不显示,请高手指点一下~~~~~~

background 简写属性在一个声明中设置所有的背景属性。

background出现无法显示通常以下几点:

    background重复使用,比如background:#fefefe;后面加background:red;

    优先级问题,(外部样式)External style sheet <(内部样式)Internal style sheet <(内联样式)Inline style

    写法错误,出现颜色和图片背景时尽量写成一句话比如:body
    background:#fefefe url('/templates/red/html/images/main.jpg') no-repeat center top; font-size:14px;

参考技术A #fefefe 已经是无限接近白色了,你换成黑色试试看。

另外css的确不能想你这么写,要学习如何精简,你这么写css文件会大很多,而且还很慢。
参考技术B 把background:#fefefe;改为background-color:#fefefe;
再定义一下高宽度,尺寸一定要大于图片尺寸,不然颜色值会被图片覆盖的!
参考技术C body
background:#fefefe url('/templates/red/html/images/main.jpg') no-repeat center top;
font-size:14px; 这样一句话就行了。

#fefefe 是白色的 你填充的是白色的吗?追问

我改成你这样的还是不行啊,不是白色,接近白色的一种颜色

追答

没有效果,你截图我看下

你把颜色改成其他颜色看下, 改成黑色的看下有效果没

本回答被提问者采纳
参考技术D 可能上面有另一个background,当前优先级不够导致被覆盖了,所以不起作用。
有代码没有?

以上是关于Thread.sleep(1)为啥不起作用?请高手帮忙分析!的主要内容,如果未能解决你的问题,请参考以下文章

如果我不在 WinForms 上使用 Thread.Sleep,BackgroundWorker 将不起作用

java中的thread.sleep(1000) 用法

为啥要放弃使用Thread.Sleep

为啥background 不起作用

为啥 Thread.Sleep 如此有害

如果 Task.Delay 优于 Thread.Sleep,为啥本书中的示例使用 Thread.Sleep?