问一个同步问题

amdgaming 2010-02-24 07:43:09
当一个线程进入一个对象的一个synchronized方法后,其它线程是否可进入此对象的其它synchronized方法?
...全文
118 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
henry_fuzr 2010-02-26
  • 打赏
  • 举报
回复
不能 ,synchronized方法的同步锁是this 指的是当前对象。
如果新建一个对象的话就可以
nobody165 2010-02-25
  • 打赏
  • 举报
回复
dafeicao 貌似你的例子有问题哦。
当一个线程进入一个对象的一个synchronized方法后,其它线程是不可进入此对象的其它synchronized方法的.
dafeicao 2010-02-25
  • 打赏
  • 举报
回复
嗯. 当时欠考虑 可是这种应该写怎样的测试呢?
LuffySY 2010-02-25
  • 打赏
  • 举报
回复
引用 8 楼 dafeicao 的回复:
Java code/**
*@author Administrator
**/publicclass ThreadTestimplements Runnable {

Test test;privateint j=1;public Test getTest() {return test;
}publicvoid setTest(Test test) {this.test= test;
}public ThreadTest(int j) {this.j= j;
}publicvoid run() {
test=Test.getTest();
test.test1(j);
test.test2(j);
test.test3(j);
test.test4(j);
}publicstaticvoid main(String[] args) {for (int i=0; i<10; i++) {new Thread(new ThreadTest(i)).start();
}


}
}class Test {privatestatic Test test=null;private Test() {
}publicstatic Test getTest() {if (test==null) {returnnew Test();
}return test;
}publicsynchronizedvoid test1(int i) {
System.out.println("is test1 Thread ="+ i);
}publicsynchronizedvoid test2(int i) {
System.out.println("is test2 Thread ="+ i);
}publicsynchronizedvoid test3(int i) {
System.out.println("is test3 Thread ="+ i);
}publicvoid test4(int i) {
System.out.println("is test4 Thread ="+ i);
}
}


请注意打印出来的is test1 Thread =9    说明其它线程可以进入其它synchronized方法
打印结果:

is test1 Thread =1
is test2 Thread =1
is test3 Thread =1
is test4 Thread =1
is test1 Thread =2
is test2 Thread =2
is test3 Thread =2
is test4 Thread =2
is test1 Thread =0
is test2 Thread =0
is test3 Thread =0
is test4 Thread =0
is test1 Thread =3
is test2 Thread =3
is test3 Thread =3
is test4 Thread =3
is test1 Thread =4
is test2 Thread =4
is test3 Thread =4
is test4 Thread =4
is test1 Thread =6
is test2 Thread =6
is test3 Thread =6
is test4 Thread =6
is test1 Thread =7
is test2 Thread =7
is test3 Thread =7
is test4 Thread =7
is test1 Thread =8
is test2 Thread =8
is test3 Thread =8
is test4 Thread =8
is test1 Thread =9
is test1 Thread =5
is test2 Thread =5
is test3 Thread =5
is test4 Thread =5
is test2 Thread =9
is test3 Thread =9
is test4 Thread =9


那个9打印出来 方法都结束了 没办法证明是进入的。 答案是 不能进入 其他synchronized方法的
feishare 2010-02-25
  • 打赏
  • 举报
回复
引用 1 楼 ant_yan 的回复:
不可以 当一个线程进入这个synchronized方法之后,就拥有了这个对象的锁,其他线程进入其他synchronized方法的条件是获得那个锁。但是其他线程可以进入非synchronized方法,因为不需要获得那个锁。

nice
龙四 2010-02-24
  • 打赏
  • 举报
回复
其他进程还能同时进那个方法的话synchronized还有什么意义?!
fatRoach 2010-02-24
  • 打赏
  • 举报
回复
“进入其它synchronized方法”的含义你搞明白了么,LS的……
dafeicao 2010-02-24
  • 打赏
  • 举报
回复
代码写的有点乱- - 见谅 等 拍
dafeicao 2010-02-24
  • 打赏
  • 举报
回复


/**
* @author Administrator
*
*/
public class ThreadTest implements Runnable {

Test test;

private int j = 1;

public Test getTest() {
return test;
}

public void setTest(Test test) {
this.test = test;
}

public ThreadTest(int j) {
this.j = j;
}

public void run() {
test=Test.getTest();
test.test1(j);
test.test2(j);
test.test3(j);
test.test4(j);
}

public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(new ThreadTest(i)).start();
}


}
}

class Test {
private static Test test = null;

private Test() {
}

public static Test getTest() {
if (test == null) {
return new Test();
}
return test;
}

public synchronized void test1(int i) {
System.out.println("is test1 Thread =" + i);
}

public synchronized void test2(int i) {
System.out.println("is test2 Thread =" + i);
}

public synchronized void test3(int i) {
System.out.println("is test3 Thread =" + i);
}

public void test4(int i) {
System.out.println("is test4 Thread =" + i);
}
}




请注意打印出来的is test1 Thread =9 说明其它线程可以进入其它synchronized方法
打印结果:

is test1 Thread =1
is test2 Thread =1
is test3 Thread =1
is test4 Thread =1
is test1 Thread =2
is test2 Thread =2
is test3 Thread =2
is test4 Thread =2
is test1 Thread =0
is test2 Thread =0
is test3 Thread =0
is test4 Thread =0
is test1 Thread =3
is test2 Thread =3
is test3 Thread =3
is test4 Thread =3
is test1 Thread =4
is test2 Thread =4
is test3 Thread =4
is test4 Thread =4
is test1 Thread =6
is test2 Thread =6
is test3 Thread =6
is test4 Thread =6
is test1 Thread =7
is test2 Thread =7
is test3 Thread =7
is test4 Thread =7
is test1 Thread =8
is test2 Thread =8
is test3 Thread =8
is test4 Thread =8
is test1 Thread =9
is test1 Thread =5
is test2 Thread =5
is test3 Thread =5
is test4 Thread =5
is test2 Thread =9
is test3 Thread =9
is test4 Thread =9
dafeicao 2010-02-24
  • 打赏
  • 举报
回复
先占个楼.要不一会找不到 了... 敲测试代码中
nj_dobetter 2010-02-24
  • 打赏
  • 举报
回复
synchronized方法,原理上说是要把对象的数据的存取原子化
sfli_g 2010-02-24
  • 打赏
  • 举报
回复
synchronized关键字就是为了处理多线程同步的

fatRoach 2010-02-24
  • 打赏
  • 举报
回复
方法的synchronized等效于

synchronized(this){
...
}
Fantastic99 2010-02-24
  • 打赏
  • 举报
回复
不可以
你用synchronized声明了该方法 如果该方法是非静态的 那么在任何一个实例中
当线程A调用该方法了 那么A就持有了锁了
其他线程再调用该实例中任何修饰了synchronized的方法便会得不到锁 会等待锁的释放
如果该方法是静态的 那么就会到类的级别。
amdgaming 2010-02-24
  • 打赏
  • 举报
回复
貌似 是不可以啊。谢谢楼上的啊
Ant 2010-02-24
  • 打赏
  • 举报
回复
不可以 当一个线程进入这个synchronized方法之后,就拥有了这个对象的锁,其他线程进入其他synchronized方法的条件是获得那个锁。但是其他线程可以进入非synchronized方法,因为不需要获得那个锁。

62,615

社区成员

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

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