今天在一位大神的博客中学习多线程的知识,其中有一段关于线程中sleep()方法的测试代码,内容如下:

public class SleepLockTest{ private static Object obj = new Object();public static void main(String[] args){ ThreadA t1 = new ThreadA("t1"); ThreadA t2 = new ThreadA("t2"); t1.start(); t2.start();} static class ThreadA extends Thread{public ThreadA(String name){ super(name); } public void run(){ // 获取obj对象的同步锁synchronized (obj) {try {for(int i=0; i <10; i++){ System.out.printf("%s: %d\n", this.getName(), i); // i能被4整除时,休眠100毫秒if (i%4 == 0)Thread.sleep(100);}} catch (InterruptedException e) {e.printStackTrace();}}} } } 

我们知道,sleep()方法会让线程进去休眠状态,但是并不释放当前所持有的同步锁(synchronized (obj)),所以这个代码最后的结果肯定是 t1 线程先执行完,然后 t2线程继续执行。但我今天发现的一个知识点是关于内部类,因为工作中很少涉及内部类,所以这段知识有些匮乏,现在发现问题,就先将这个小问题整理一下。


当我试图将 内部类ThreadAstatic 关键字去掉的时候,编译器会在new ThreadA的时候告诉我这个错:

No enclosing instance of type SleepLockTest is accessible. Must qualify the allocation with an enclosing instance of type SleepLockTest (e.g. x.new A() where x is an instance of SleepLockTest).

这句话的大概意思是:没有可访问的内部类SleepLockTest 的实例,必须分配一个合适的内部类SleepLockTest 的实例(如x.new A(),x必须是SleepLockTest 的实例。)
这是因为我是在static void main 方法中 试图创建一个非静态内部类,但是在java中,类中的静态方法不能直接调用动态方法(调用非静态内部类ThreadA的构造方法)。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法。所以要将static 关键字加上,或者将new ThreadA 放到main 方法外面的非静态区域中。

No enclosing instance of type * is accessible. Must qualify the allocation with an enclosing instanc相关推荐

  1. 关于“No enclosing instance of type * is accessible. Must qualify the allocation with an enclo……”的总结与思考

    报错的完整信息: No enclosing instance of type OutterClass is accessible. Must qualify the allocation with a ...

  2. 解决No enclosing instance of type * is accessible

    写一个内部类,并在构造函数中初始化时,遇到报错,搜索问题后发现,有网友出现过类似的问题,下面这个是说的浅显明白的,并确实解决了问题.于是,以下内容照搬过来,不再多费键盘了. public class ...

  3. No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing

    No enclosing instance of type SmsUtils is accessible. Must qualify the allocation with an enclosing ...

  4. No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instanc

    java : No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing ...

  5. 语法错误:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing

    错误 问题 No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing 没 ...

  6. No enclosing instance of type X is accessible. Must qualify the allocation with an enclosing instanc

    No enclosing instance of type X is accessible. Must qualify the allocation with an enclosing instanc ...

  7. No enclosing instance of type Main is accessible. Must qualify the allocation with an enclosing i...

    最近在刷AC,在编译时出现: No enclosing instance of type Main is accessible. Must qualify the allocation with an ...

  8. enclosing type java_java中no enclosing instance of type * is accessible的解决方法

    这种情况一般发生在"在静态方法里面使用内部类" 测试代码: public class Test { public static void main(String[] args) { ...

  9. Java出现No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosin

    最近研究LDA源代码时,里面涉及到Comparable方法的使用.以前用过这个排序方法,现在想回顾一下.以下是程序,感觉没问题啊,结果报错了: Exception in thread "ma ...

最新文章

  1. IntelliJ IDEA 2021.3.2 发布:告别不断建议安装xx插件的提示!
  2. java优先队列的入队函数_Java内置的优先队列PriorityQueue
  3. JpaSpecificationExecutor
  4. egg(110,111,112)--egg之微信支付
  5. map在Java集合_java集合之Map
  6. .net mvc actionresult 返回字符串_Spring 框架基础(06):Mvc架构模式简介,执行流程详解
  7. 7.2版本中安装的插件都是本插件未被启用???
  8. 洛谷——P1311 选择客栈
  9. Domino XML Language(DXL)简介
  10. 算法动画图解(安卓)、排序算法的可视化
  11. VARCHART XGantt用户手册:如何甘特图中的数据表
  12. 拼多多无货源商家一键上传同行店铺宝贝软件?
  13. ubuntu服务器系统安装教程,Ubuntu--服务器版本系统安装图解教程
  14. speedoffice如何在Word表格中插入行或列
  15. tortoise清理本地分支_如何删除TortoiseHg中的意外分支?
  16. MediaWiki安装插件Semantic MediaWIKI + PageForms
  17. python判断字符串包含中文、数字、英文
  18. [一场梦转无踪 为什么美梦有始无终]香格里拉夏宫自助茶点
  19. VScode直接执行ts文件
  20. 一招技巧解决360搜索结果页展示网站LOGO

热门文章

  1. DeprecationWarning: Seeding based on hashing is deprecated since Python 3.9
  2. OpenCV中Viz模块的安装(VS2015)
  3. 【你好,windows】win7(2019版)旗舰纯净版(usb3.0_nvme)2020.3.14
  4. HDU-3374 String Problem
  5. vmware fusion安装kvm虚拟机
  6. MSF(Metasploit)渗透测试---黑客工具使用指南
  7. 天津医科大学计算机研究生吧,天津医科大学
  8. Win7怎么进入安全模式改密码
  9. 网站Logo SEO优化
  10. 武汉计算机软件应届毕业生工资,精打细算告诉你一个应届毕业生在武汉工资多少才能活下来(汉口物件)...