thread05
来源:互联网 发布:剑灵天剑士捏脸数据 编辑:程序博客网 时间:2024/05/22 01:51
package com.neutron.t05;/** * 问题:同步方法和非同步方式是否可以同时调用? * 答案:同步方法和非同步方法是可以同时使用的。 */public class T05 { public synchronized void run1() { System.out.println(Thread.currentThread().getName() + " start"); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " end"); } public void run2() { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " best"); } public static void main(String[] args) { T05 t05 = new T05(); new Thread(() -> t05.run1(), "run1").start(); new Thread(() -> t05.run2(), "run2").start();// new Thread(t05::run1, "run1").start();// new Thread(t05::run2, "run2").start(); }}/* 过程解说: 1.执行run1方法需要锁定当前对象的,而执行第二个方法是不需要锁定当前对象的,在执行run1的方法时,是否会执行run2方法呢? 类比现实,家里只有一个浴室,那么去了一个人洗澡,第二个人只能去等待第一个人洗完才能去洗澡。但现在的情景是1个人去洗澡,1个人去吃饭, 两者没有任何冲突和影响。 */
阅读全文
0 0
- thread05
- 本地文件的读和写
- Linux C编程学习笔记(3):read、write、lseek函数及文件读写和文件读写指针的移动操作
- 261. Graph Valid Tree
- 设计模式
- 323. Number of Connected Components in an Undirected Graph
- thread05
- Mysql大小写敏感问题
- 高考就算失败了又如何?
- Centos7设置UTF-8编码,防止中文乱码
- OBS斗鱼直播弹幕插件效果
- oracle数据库开发注意事项
- Request.QueryString["id"]与Request["id"]区别
- thread06
- openjudge ----- 流传感染