日期:2014-05-20 浏览次数:20640 次
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()); } }
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()); } }