关于 Thread.currentThread()

currentThread()  到底是什么? 其实currentThread() 只是Thread 的一个静态方法。返回的正是执行当前代码指令的线程引用:

    /*** Returns a reference to the currently executing thread object.** @return  the currently executing thread.*/public static native Thread currentThread();

换句话说, Thread.currentThread() 返回的是 一个实例。 只不过呢, 这个实例确实比较特殊。 这个实例是当前Thread 的引用。Thread.currentThread() 当然 不等于 Thread。

问题1 :

之前一直存在的一个问题是:

Thread.currentThread().sleep(x)

Thread.sleep(x)

两者到底有什么区别,Thread.currentThread() 这样的写法是必须的吗。 到底哪种写法更好呢?

那为什么我们有常常看到:

Thread.currentThread().sleep(2000);

这样的用法呢??

其实,如果我们代码这么写:

Thread.currentThread().sleep(2000);

会有下面的提示, 当然这个提示也不算是什么错误。但是总归是不太好的。因为这个提示有点警告的意味。

Static member 'java.lang.Thread.sleep(long)' accessed via instance reference less... (Ctrl+F1)
Shows references to static methods and fields via class instance rather than a class itself.

它 的意思是说, 你正在通过一个对象实例来调用它的静态方法。 (一般来说, 这确实是不好的编程实践。)

但是改成下面这样就好了:
Thread.sleep(2000);

但是,当年,我看视频看别人代码,都是这么写的。 事实上,虽然两者效果完全一样,都没错,但是还是细微差别的。那么,哪个是最佳实践呢? 答案是后者: Thread.sleep(x) 

本质上来讲, 其实是没有区别的,其功效完全一样。不过呢, 一些代码检查工具会认为  前者有点问题。

While you can call a static method via an instance reference, it's not good style ———— 这个就是代码检查工具提示判断的 依据。 不是一个好的 style。

Thread.currentThread().sleep(2000); 这样 就可以让这行代码看起来 达到了某些程序员的“原本的心意”。 虽然这样做是无益的,但是也是无害的。 总之,它可以避免某些问题,可以避免某些程序员出现低级错误。。。  这个其实是早期程序员的一个通用的写法, 无益也无害。 已经成为了一个不那么好的习俗了吧

26down vote

In Java, sleep is a static method. Both your examples do exactly the same thing, but the former version is confusing because it looks like it is calling a method on a specific object but it's not doing that at all. In your example it won't matter much, but it is more dangerous if you have the following:

someOtherThread.sleep(x);

This time it looks like you are telling some other thread to sleep, but in fact you are putting the current thread to sleep. The way to avoid making this type of mistake is to always call static methods using the class rather than a specific object.

The way to avoid making this type of mistake is to always call static methods using the class rather than a specific object. —— 避免了“调用一个实例的静态方法,而实际上应该是调用一个类的静态方法” 之类的错误。 其实说白了也就是避免这样的错误:   someOtherThread.sleep(x);

问题2:

还有一个问题是: Thread.currentThread()与this的区别

在线程的run 方法内部, Thread.currentThread()与this 其实是一个意思(除非你手动执行run方法,而不是通过启动线程的方式)。 不过呢, 其他的某些方法或场合,却可能出现不一致。

一般来说,Thread.currentThread() 用于不方便使用this 的场合。 Thread.currentThread() 明确表明了它所针对的对象是 当前线程! 不是this! 当然,如果碰巧this 就是当前线程, 那也是没毛病的。

参考:

http://blog.csdn.net/yezis/article/details/57513130

https://stackoverflow.com/questions/2077216/java-thread-currentthread-sleepx-vs-thread-sleepx

posted on 2017-09-15 19:08 CanntBelieve 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/FlyAway2013/p/7527916.html

关于 Thread.currentThread()相关推荐

  1. Thread.currentThread().getContextClassLoader() 和 Class.getClassLoader()区别

    查了一些资料也不是太明白两个的区别,但是前者是最安全的用法 打个简单的比方,你一个WEB程序,发布到Tomcat里面运行. 首先是执行Tomcat org.apache.catalina.startu ...

  2. Thread.currentThread().getContextClassLoader().getResourceAsStream()读取配置文件

    Java中使用的路径,分为两种:绝对路径和相对路径.具体而言,又分为四种: 一.URI形式的绝对资源路径 如:file:/D:/java/eclipse/workspace/j/bin/a URL是U ...

  3. 演示Thread.sleep(100)和Thread.currentThread().isInterrupted()+@Deprecated:将方法标注为废弃的方法...

    package charpter08; public class TestInterrupt01 { public static void main(String[] args) { Processo ...

  4. android getid,Process.myTid()和Thread.currentThread().getId()区别

    首先,两个方法都是返回线程ID,但结果是不同的,简单的说 android.os.Process.myTid()是系统级的ID Thread.currentThread().getId()是Java级的 ...

  5. Thread.currentThread().getContextClassLoader()和Class.getClassLoader()区别

    2019独角兽企业重金招聘Python工程师标准>>> What is different between Thread.currentThread().getContextClas ...

  6. Thread.currentThread()方法、进程、线程、多线程相关总结(二)

    Thread.currentThread()方法 Thread.currentThread()可以获取当前线程的引用,一般都是在没有线程对象又需要获得线程信息时通过Thread.currentThre ...

  7. Thread.currentThread()、isAlive()、Thread.sleep()的使用

    1.Thread.currentThread()方法: 返回当前正在运行的线程 一个简单的例子: MyThread_7.Java类的构造函数是被main线程调用的,而run()方法是被名为Thread ...

  8. Thread.currentThread.interrupt()

    Thread.currentThread.interrupt() 只对阻塞线程起作用, 当线程阻塞时,调用interrupt方法后,该线程会得到一个interrupt异常,可以通过对该异常的处理而退出 ...

  9. Thread.currentThread().interrupt()

    https://www.zhihu.com/question/41048032 作者:Intopass 链接:https://www.zhihu.com/question/41048032/answe ...

最新文章

  1. FPGA设计细节和实现(初学者)
  2. 什么是CGI、FastCGI、PHP-CGI、PHP-FPM
  3. Redis 快速入门
  4. Trie树kmpAC自动机后缀数组Manacher
  5. 面试了3个‘85前’的嵌入式软件工程师
  6. 批处理 如果提示错误暂停_dos批处理脚本代码,一键删除目录文件夹例子,dos命令bat教程...
  7. 360浏览器,打开一个,为什么后台有多个360进程?
  8. pixi 小游戏_学习如何用pixi.js开发微信小游戏
  9. Reincarnation HDU - 4622 (后缀自动机)
  10. w ndows热键,Window 10 优雅的快捷键
  11. Vue学习(学习打卡Day14)
  12. python+opencv别踩白块儿游戏辅助,一天一个opencv小项目(已开源)
  13. 现代都市女性所爱的潮流搭!
  14. 三角形(triangle)
  15. EVE模拟器的安装使用
  16. 【多线程】多线程案例
  17. react 中使用百度地图
  18. [相机配置] 海康相机丢包配置环境
  19. 线上学微积,百度少不了
  20. 命令行查看笔记本电脑电池使用状态

热门文章

  1. 企业内部控制与全面风险管理体系建设案例解析
  2. 珊瑚海-一站式动态化布局框架
  3. CNN 卷积神经网络-- 残差计算
  4. c语言,函数声明的误区
  5. VScode 安装中文插件,不生效的解决办法
  6. 图解正则表达式,这一篇就够了
  7. java实现销售预测算法,预测5天销售
  8. 强一致共识算法-BFT/CFT
  9. 我见过最NB的鼠标-鼠标放进PC卡插槽 惠普卡片蓝牙鼠试用
  10. 用平面图片制作3D模型【3DsMax】