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

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

关于 Thread.currentThread()的更多相关文章

  1. Java多线程之this与Thread.currentThread()的区别——java多线程编程核心技术

      package mythread; public class CountOperate extends Thread{ public CountOperate(){ System.out.prin ...

  2. 关于Thread.currentThread()和this的差异

    重新来看多线程时,被这结果搞懵逼了.不多说,直接上代码: public class MyThread02 extends Thread { public MyThread02() { System.o ...

  3. ClassLoader,Thread.currentThread().setContextClassLoader,tomcat的ClassLoader

    实际上,在Java应用中所有程序都运行在线程里,如果在程序中没有手工设置过ClassLoader,对于一般的java类如下两种方法获得的ClassLoader通常都是同一个 this.getClass ...

  4. Class.forName() 初始化、Thread.currentThread().getContextClassLoader().getResourceAsStream

    Class.forName() 和 ClassLoader.loadClass()的区别? Class.forName() 和 Class.forName().NewInstance()的区别? Cl ...

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

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

  6. String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) 获取项目的绝对路径(shiro项目中来的八)

    一,上代码 String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) file: ...

  7. Not on FX application thread; currentThread = AWT-EventQueue-0的解决方法

    swing awt跑javafx报了这问题 Not on FX application thread; currentThread = AWT-EventQueue-0 解决方法 Platform.r ...

  8. Thread.currentThread()和this的区别——《Java多线程编程核心技术》

    前言:在阅读<Java多线程编程核心技术>过程中,对书中程序代码Thread.currentThread()与this的区别有点混淆,这里记录下来,加深印象与理解. 具体代码如下: pub ...

  9. Thread.currentThread().getContextClassLoader().getResourceAsStream

    Thread.currentThread().getContextClassLoader().getResourceAsStream 2014年04月02日 06:49:47 OkidoGreen 阅 ...

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

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

随机推荐

  1. jQuery控制元素显示、隐藏、切换、滑动的方法

    jQuery 隐藏和显示 通过 hide() 和 show() 两个函数,jQuery 支持对 HTML 元素的隐藏和显示: 实例 $("#hide").click(functio ...

  2. 【git】之clone(克隆)

    直接克隆 git clone https://github.com/gyjx/test.git 指定克隆某个分支 git clone -b dev https://github.com/gyjx/te ...

  3. P1072Hankson的趣味题

    传送 这个题一本通上有,但是为了增强我们的创新精神,思维能力balabala,书上的满分程序不全,要优化一下,在此写一下第二种方法 #include<iostream> #include& ...

  4. java设计模式-Iterator

    Iterator模式 主要是用在容器的遍历上,其他的地方都不怎么用:理解一下,会用了就可以了:   1.背景 请动手自己写一个可以动态添加对象的容器: 代码: ArrayList.java(是自己实现 ...

  5. PAT 乙级 1068 万绿丛中一点红(20 分)

    1068 万绿丛中一点红(20 分) 对于计算机而言,颜色不过是像素点对应的一个 24 位的数值.现给定一幅分辨率为 M×N 的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的 ...

  6. Linux rpm包安装MySQL数据库问题总结

    1.安装包准备 2.按顺序安装以下安装包 一定要按顺序安装,因为前面的包是后面包的依赖 [root@iz2ze1bzpi3orra8lboxqiz mysql]# rpm -ivh Percona-S ...

  7. Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素

    ylbtech-Java-Runoob-高级教程-实例-数组:14. Java 实例 – 在数组中查找指定元素 1.返回顶部 1. Java 实例 - 在数组中查找指定元素  Java 实例 以下实例 ...

  8. UnicodeString基本操作(Ring3)

    // Unicode_String_Ring3.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "Unicode ...

  9. DockerFile服务

    Dockerfile分为四部分:基础镜像信息.镜像创建者信息.镜像操作指令.容器启动执行指令. 一.Dockerfile的书写规则及指令使用方法 Dockerfile的指令是忽略大小写的,建议使用大写 ...

  10. python之路——2

    王二学习python的笔记以及记录,如有雷同,那也没事,欢迎交流,wx:wyb199594 复习 1.编译型:一次性将全部的代码编译成二进制文件 c c++ 优点:运行效率高 缺点:开发速度慢,不能跨 ...