Java并发

来源:互联网 发布:低学历大牛程序员 编辑:程序博客网 时间:2024/05/08 23:29

package Thread;

public class SynTest implements Runnable{
 int number = 1;
 public synchronized void m1() throws Exception{

  number = 2;
  Thread.sleep(500);
  System.out.println("m1:number:"+number);
  
 }
 
 public synchronized void m2()throws Exception{
  System.out.println("@m2");
  Thread.sleep(250);
  number = 3;
 // System.out.println("@m2:number:"+number);
 }
 
 public void run(){
  try{
  m1();
  }
  catch(Exception e)
  {
   System.out.println(e);
  }
 }
 

 public static void main(String []args) throws Exception
 {
  SynTest st = new SynTest();
  Thread t = new Thread(st);
 // st.m2();
  t.start();
  st.m2();
  t.sleep(250);
  System.out.println(st.number);
  
 }
 

}

原创粉丝点击