import java.util.*;public class Syncduixiang implements Runnable{
int b=1000;
public static void main(String []args) throws Exception{
Syncduixiang ss=new Syncduixiang();
Thread s1=new Thread(ss);
System.out.println(new Date());
s1.start();
ss.m2();
System.out.println("b="+ss.b+new Date());
}
public void run(){
try{
m1();
}catch(Exception e){
}
}
public synchronized void m1 ()throws Exception{
System.out.println("m1先执行");
int a=3000;
Thread.sleep(5000);
System.out.println("m1a="+a+new Date());
}
public synchronized void m2 ()throws Exception{
System.out.println("m2先执行");
b=4000;
Thread.sleep(2000);
System.out.println("m2b="+b+new Date());
}
}
运行结果是
Tue Sep 13 20:29:20 CST 2011
m2先执行
m2b=4000Tue Sep 13 20:29:22 CST 2011
b=4000Tue Sep 13 20:29:22 CST 2011
m1先执行
m1a=3000Tue Sep 13 20:29:27 CST 2011为什么m1和m2 之间会阻塞 他们之间没冲突啊
int b=1000;
public static void main(String []args) throws Exception{
Syncduixiang ss=new Syncduixiang();
Thread s1=new Thread(ss);
System.out.println(new Date());
s1.start();
ss.m2();
System.out.println("b="+ss.b+new Date());
}
public void run(){
try{
m1();
}catch(Exception e){
}
}
public synchronized void m1 ()throws Exception{
System.out.println("m1先执行");
int a=3000;
Thread.sleep(5000);
System.out.println("m1a="+a+new Date());
}
public synchronized void m2 ()throws Exception{
System.out.println("m2先执行");
b=4000;
Thread.sleep(2000);
System.out.println("m2b="+b+new Date());
}
}
运行结果是
Tue Sep 13 20:29:20 CST 2011
m2先执行
m2b=4000Tue Sep 13 20:29:22 CST 2011
b=4000Tue Sep 13 20:29:22 CST 2011
m1先执行
m1a=3000Tue Sep 13 20:29:27 CST 2011为什么m1和m2 之间会阻塞 他们之间没冲突啊
下面是Thread的run方法,target是你穿进去的Runnable对象
public void run() {
if (target != null) {
target.run();
}
}
实际上
s1.start();
ss.m2();
最终还是一个对象调用的 分别调用m1和m2
我把public class Syncduixiang implements Runnable
改成public class Syncduixiang extends Thread
结果还是一样 。2个线程还是有阻塞
但是我把m1 和 m2 方法放进另外一个类,他们却可以同时运行。这是怎么搞的。
就是比如定义一个类 class ff{
public synchronized void m1 ()throws Exception{
System.out.println("m1先执行");
int a=3000;
Thread.sleep(3000);
System.out.println("m1a="+a+new Date());
}
public synchronized void m2 ()throws Exception{
System.out.println("m2先执行");
b=4000;
Thread.sleep(2000);
System.out.println("m2b="+b+new Date());
}
}然后在主程序里 调用 ff 类的方法m1 m2 这样的话 运行起来 他们就不阻塞了
int b=1000;
public static void main(String []args) throws Exception{
Syncduixiang ss=new Syncduixiang();
Thread s1=new Thread(ss);
System.out.println(new Date());
s1.start();
Ff f1=new Ff();
f1.m2();
System.out.println("b="+ss.b+new Date());
}
public void run(){
try{
Ff f2=new Ff();
f2.m1();
}catch(Exception e){
}
}
}class Ff{
public synchronized void m1 ()throws Exception{
System.out.println("m1先执行");
int a=3000;
Thread.sleep(3000);
System.out.println("m1a="+a+new Date());
}
public synchronized void m2 ()throws Exception{
System.out.println("m2先执行");
Syncduixiang q=new Syncduixiang();
q.b=4000;
Thread.sleep(2000);
System.out.println("m2b="+q.b+new Date());
}
}
第一个对象 Ff f1=new Ff(); 调用的m2
第二个对象 Ff f2=new Ff(); 调用的m1你可以尝试
import java.util.*;public class Syncduixiang extends Thread{
int b=1000;
Ff f1=new Ff();
public static void main(String []args) throws Exception{
Syncduixiang ss=new Syncduixiang();
System.out.println(new Date());
ss.start();
ss.f1.m2();
System.out.println("b="+ss.b+new Date());
}
public void run(){
try{
f1.m1();
}catch(Exception e){
}
}
}class Ff{
public synchronized void m1 ()throws Exception{
System.out.println("m1先执行");
int a=3000;
Thread.sleep(3000);
System.out.println("m1a="+a+new Date());
}
public synchronized void m2 ()throws Exception{
System.out.println("m2先执行");
Syncduixiang q=new Syncduixiang();
q.b=4000;
Thread.sleep(2000);
System.out.println("m2b="+q.b+new Date());
}
}