深圳尚学堂:请说出你所知道的线程同步的方法

深圳尚学堂:请说出你所知道的线程同步的方法

编码文章call10242025-04-26 17:01:1511A+A-

1、实现线程安全的两种方式

1)synchronized方法:通过在方法声明中加入synchronized关键字来声明

synchronized方法。

2)synchronized块:通过synchronized关键字来声明synchronized块。

2、薪水排序后薪水排名在第3-5的员工

1)select * from(select ename,sal,rownum rn from

(select ename,sal from emp_44 where sal is not null order by sal desc) where rownum<6)where rn>2;

2)select * from(select ename,sal,rownum rn from

(select ename,sal from emp_44 where sal is not null order by sal desc))where rn between 3 and 5;

3、请说出你所知道的线程同步的方法。

wait():使一个线程处于等待状态,并且释放所持有的对象的lock;

sleep():使一个正在运行的线程处于睡眠状态,是一个静态方法,调用此方法要 捕捉InterruptedException异常;

notify():唤醒一个处于等待状态的线程,注意的是在调用此方法的时候,并不能确切的唤醒某一个等待状态的线程,而是由JVM确定唤醒哪个线程,而且不是按优先级;

notityAll():唤醒所有处入等待状态的线程,注意并不是给所有唤醒线程一个对象的锁,而是让它们竞争。

更多问题,请移步深圳尚学堂官网:www.0755sxt.com

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4