我们先来看一下实例代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
class threada extends thread{
public threada(string name) {
super (name);
}
public void run() {
synchronized ( this ) {
system.out.println(thread.currentthread().getname()+ " call notify()" );
notify();
}
}
}
public class waittest {
public static void main(string[] args) {
threada t1 = new threada( "t1" );
synchronized (t1) {
|
输出结果:main start t1 -> main wait() -> t1 call notify() -> main continue
其实调用t1.start(),t1为就绪状态,只是main方法中,t1被main线程锁住了,t1.wait()的时候,让当前线程等待,其实是让main线程等待了,然后释放了t1锁,t1线程执行,打印t1 call notify(),然后唤醒main线程,最后结束;
这里说一下wait()与sleep()的区别,他们的共同点都是让线程休眠,但是wait()会释放对象同步锁,而sleep()不会;下面的代码t1结束之后才会运行t2;能够证实这一点;
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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(){
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();
}
}
}
}
}
|
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 32
-
2025-05-27 46
-
2025-05-29 90
-
2025-05-29 68
-
2025-05-25 65
热门评论