java 多线程 两个线程访问两个对象中不同的synchronized修饰的方法。(方法和对象都是同一个类的)
public static void main(String[] args){
Test m1 = new Test();
Test m2 = new Test();
TestSynchronized_1 s1 = new TestSynchronized_1(m1);
TestSynchronized_2 s2 = new TestSynchronized_2(m2);
new Thread(s1,"t1").start();
new Thread(s2,"t2").start();
}
}
class Test{
synchronized public void test1(){
for(int p = 0; p < 5; p++){
System.out.println("s1.run.TestSynchronized_test 1");
}
}
synchronized public void test2(){
for(int p = 0; p < 5; p++){
System.out.println("s2.run.TestSynchronized_test 2");
}
}
}
class TestSynchronized_1 implements Runnable{
private Test m;
public TestSynchronized_1(Test m){
this.m = m;
}
public void run(){
m.test1();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class TestSynchronized_2 implements Runnable{
private Test m;
public TestSynchronized_2(Test m){
this.m = m;
}
public void run(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
m.test2();
}
}
运行结果:
s1.run.TestSynchronized_test 1
s1.run.TestSynchronized_test 1
s1.run.TestSynchronized_test 1
s1.run.TestSynchronized_test 1
s1.run.TestSynchronized_test 1
s2.run.TestSynchronized_test 2
s2.run.TestSynchronized_test 2
s2.run.TestSynchronized_test 2
s2.run.TestSynchronized_test 2
s2.run.TestSynchronized_test 2
这个是两个线程访问不同对象中不同的synchronized方法为什么还会受到synchronized的限制,这个如何理解?
会的大神麻烦讲下运行的详细过程,谢谢~ 展开
先上结论:两个线程访问不同对象中不同的synchronized方法不会受到synchronized的限制。
程序运行结果之所以这样,原因在于run()方法的实现导致的。
线程1中,先调用m.test1(),接着Thread.sleep(1000)
线程2中,先Thread.sleep(1000),接着调用m.test2()
主程序中,线程1与2都有可能先开始,无论谁先开始,结果都是线程1中m.test1()先结束,线程2中m.test2()后结束。
因为轮到线程2时,先Thread.sleep(1000),将资源让给线程1
改进方法,应该在打印时,让线程sleep
class Test{
synchronized public void test1(){
for(int p = 0; p < 5; p++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("s1.run.TestSynchronized_test 1");
}
}
synchronized public void test2(){
for(int p = 0; p < 5; p++){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("s2.run.TestSynchronized_test 2");
}
}
}
run()方法中直接调用即可
class TestSynchronized_1 implements Runnable{
private Test m;
public TestSynchronized_1(Test m){
this.m = m;
}
public void run(){
m.test1();
}
}
class TestSynchronized_2 implements Runnable{
private Test m;
public TestSynchronized_2(Test m){
this.m = m;
}
public void run(){
m.test2();
}
}
输出结果如下:
s2.run.TestSynchronized_test 2
s2.run.TestSynchronized_test 2
s1.run.TestSynchronized_test 1
s1.run.TestSynchronized_test 1
s2.run.TestSynchronized_test 2
s1.run.TestSynchronized_test 1
s2.run.TestSynchronized_test 2
s1.run.TestSynchronized_test 1
s2.run.TestSynchronized_test 2
首先谢谢你的解答,你这个我试了下确实可以。我还有一个疑问就是我在Test类中两个方法都不加sleep,run方法中也不加sleep,为什么运行后还是像问题中所述的结果一样,好像是收到了限制一样。
ps:我把里面的循环次数增多而看出了差异性,之前是线程太快了,直接就运行完了。没看出来,问题已经完美解决,谢谢你!