java学习

java学习

 

java同步锁的使用3

在一个对象中的多个方法上都加上synchronized,代表同时执行这些方法时,是同步的,同步锁是属于对象的不是单个方法的。
package test;
public class Test6 {
public  synchronized void get1(String s){
System.out.println(s);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public  synchronized void get2(String s){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(s);
}
public static void main(String[] args) {
final Test6 t =new Test6();
new Thread(new Runnable() {
@Override
public void run() {
t.get1("a");
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
t.get2("b");
}
}).start();
}
}

posted on 2018-05-02 16:35 杨军威 阅读(126) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜