java 同步块疑惑

默默坚持 2014-12-02 11:12:33
自己在学习多线程,synchronized关键字能获取对象的锁,但我测试结果让我还疑惑,同时有2个thread都能进入同步块中,但如果不使用wait、notify,就只能就一个thread进入同步块,请熟悉多线程的牛人帮忙解答下。

public class test {
public static void main(String[] args) {
Object lockObject = new Object();
ThreadT threadT = new ThreadT("thread test1",lockObject);
ThreadT threadW = new ThreadT("thread test2",lockObject);
Thread thread1 = new Thread(threadT);
Thread thread2 = new Thread(threadW);
thread1.start();
thread2.start();
}


}

class ThreadT implements Runnable{
private String name = "";
private Object objectlock = null;
public ThreadT(String name,Object objectlock){
this.name = name;
this.objectlock = objectlock;
}
@Override
public void run() {
synchronized(objectlock){
// TODO Auto-generated method stub
try {
System.out.println(name+" get the lock");
objectlock.wait(1000);
System.out.println(name+" running.......");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
objectlock.notify();
}
}
}
}

输出结果:
thread test1 get the lock
thread test2 get the lock
等待1秒后输出
thread test1 running.......
thread test2 running.......

如果把run方法修改为,

public void run() {
synchronized(objectlock){
// TODO Auto-generated method stub
//try {
System.out.println(name+" get the lock");
// objectlock.wait(3000);
System.out.println(name+" running.......");
//} catch (InterruptedException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
//}
//finally{
// objectlock.notify();
//}
}
}
执行结果为:
thread test1 get the lock
thread test1 running.......
thread test2 get the lock
thread test2 running.......
...全文
258 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
默默坚持 2014-12-04
  • 打赏
  • 举报
回复
谢谢大家的帮忙,我分给等级最高的云上飞翔了,希望你多帮助其它有疑问的程序猴!
云上飞翔 2014-12-02
  • 打赏
  • 举报
回复
引用 7 楼 jiangpan007 的回复:
objectlock.wait(2000)执行后,当前线程(线程A)挂起,等2000毫秒会尝试获取对象锁吧
不是。是立即释放对象锁,然后处于等待状态2000毫秒,时间到了,结束这个等待状态,然后试图再尝试获取锁,以便继续执行wait之后的语句。
默默坚持 2014-12-02
  • 打赏
  • 举报
回复
还想问个问题,objectlock.wait(2000)执行后,当前线程(线程A)挂起,等2000毫秒会尝试获取对象锁吧,如果还没有到达2000毫米,另一线程(线程B)执行notify,线程A是立即获得对象锁吗,还是必须等待2000毫秒后才获得对象锁; 另外我测试,假如线程A挂起200毫秒,B线程先sleep 1000毫秒再释放对象锁,线程A会再1000毫秒后获得对象锁,是不是可以这样理解,当200毫秒后,线程A就一直尝试获得对象锁,直到线程B释放对象锁。
默默坚持 2014-12-02
  • 打赏
  • 举报
回复
调用wait方法会堵塞当前线程是吧,如果2个线程都释放了对象锁,又都没能notify,这是不是死锁。 我把代码改成下面这样,不指定wait时间,运行程序后程序一直运行,并且永远没执行到打印running这行代码。 public void run() { synchronized(objectlock){ // TODO Auto-generated method stub try { System.out.println(name+" get the lock"); objectlock.wait(); System.out.println(name+" running......."); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ objectlock.notify(); } } }
默默坚持 2014-12-02
  • 打赏
  • 举报
回复
我好像不能再修改文本了
程序员Monkey 2014-12-02
  • 打赏
  • 举报
回复
希望楼主发代码的时候能够将其格式转换为java格式,不然这样格式的代码看着就不舒服,别人怎么会耐心给你解答呢,just suggestion
默默坚持 2014-12-02
  • 打赏
  • 举报
回复
thread test1 get the lock thread test2 get the lock 程序打印出这两句很快的,执行wait(1000)并不会立即释放对象锁,而是要等同步块执行完后再释放,我理解不了你的说法。能分析下执行顺序吗
skgary 2014-12-02
  • 打赏
  • 举报
回复
楼主对 wait理解错了。 wait的时候,锁是释放的状态。
karlpan01 2014-12-02
  • 打赏
  • 举报
回复
test1 wait后释放了对象锁,test2得到对象锁,test2 wait又释放了,然后test1 print ,然后test2 print

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧