java 多线程(二)synchronized

package com.alibaba.threadTest;

public class TestSyncMethod implements Runnable{

   int b=200;
    
   public void m1() throws InterruptedException{
    b=1000;
    Thread.sleep(3000);
    System.out.println( "m1.b= "+ b);
        
  }
  @Override
   public void run() {
     // TODO Auto-generated method stub
     try {
      m1();
    } catch (InterruptedException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
  }
    
   public void m2() throws InterruptedException{
    b=2000;
    Thread.sleep(2000);
    System.out.println( "m2.b= "+ b);
    
  }

   public static void main(String[] args) throws InterruptedException{
    TestSyncMethod tt= new TestSyncMethod();
    Thread th = new Thread(tt);
    
    th.start();
    tt.m2();
    
  }

}

 
输出:
 
m2.b= 1000
m1.b= 1000
 
package com.alibaba.threadTest;

public class TestSyncMethod implements Runnable{

   int b=200;
    
   public     synchronized void m1() throws InterruptedException{
    b=1000;
    Thread.sleep(3000);
    System.out.println( "m1.b= "+ b);
        
  }
  @Override
   public void run() {
     // TODO Auto-generated method stub
     try {
      m1();
    } catch (InterruptedException e) {
       // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
  }
    
   public synchronized void m2() throws InterruptedException{
    b=2000;
    Thread.sleep(2000);
    System.out.println( "m2.b= "+ b);
    
  }

   public static void main(String[] args) throws InterruptedException{
    TestSyncMethod tt= new TestSyncMethod();
    Thread th = new Thread(tt);
    
    th.start();
    tt.m2();
    
  }

}

 
输出 :
 
m2.b= 2000
m1.b= 1000
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~・
Java里可以使用synchronized来同步代码块或者方法。

当一个类中有多个synchronized方法时,如果有一个线程访问其中某个synchronized方法时,直到该方法执行完毕,其它线程对其它synchronized方法的访问也将受到阻塞。
 
同理,synchronized (obj) 表示若多个线程同时访问时,只让其中一个线程最先取得obj对象并对其加锁,其它线程则阻塞直到取得obj对象的线程执行完代码块,此时被加锁的obj对象得到释放(解锁),其它线程得到通知取得该book对象继续执行。
举例如下:
 
public class HelloSynchronized {        
    
         public static void main(String[] args) {        
                 //        
                HelloSynchronized helloSynchronized = new HelloSynchronized();        
                 //创建2个线程t1, t2,分别调用HelloSynchronized helloSynchronized的2个方法method1,与method2        
                Thread t1 = new Thread( new HelloSynchronizedRunnalbe(helloSynchronized, "method1"), "t1");        
                Thread t2 = new Thread( new HelloSynchronizedRunnalbe(helloSynchronized, "method2"), "t2");        
                        
                t1.start();        
                t2.start();        
    
        }        
    
         //synchronized public void method1() {        //同步方法        
         public void method1() {        
                 synchronized ( this) {         //同步块        
    
                        System.out.println(Thread.currentThread().getName()        
                                        + " enter method1");        
                         try {        
                                Thread.sleep(3000);        
                        } catch (InterruptedException e) {        
                                 // do nothing        
                        }        
                        System.out.println(Thread.currentThread().getName()        
                                        + " exit method1");        
                }        
        }        
    
         //synchronized public void method2() {        //同步方法        
         public void method2() {        
                 synchronized ( this) {         //同步块        
                        System.out.println(Thread.currentThread().getName()        
                                        + " enter method2");        
                         try {        
                                Thread.sleep(3000);        
                        } catch (InterruptedException e) {        
                                 // do nothing        
                        }        
                        System.out.println(Thread.currentThread().getName()        
                                        + " exit method2");        
                }        
        }        
}        
    
class HelloSynchronizedRunnalbe implements Runnable {        
         private HelloSynchronized helloSynchronized;        
         private String methodName;        
         public HelloSynchronizedRunnalbe(HelloSynchronized helloSynchronized, String methodName) {        
                 this.helloSynchronized = helloSynchronized;        
                 this.methodName = methodName;        
        }        
         public void run() {        
                 if (methodName.equals( "method1")) {        
                        helloSynchronized.method1();        
                } else if (methodName.equals( "method2")) {        
                        helloSynchronized.method2();        
                }        
        }        
}    

你可能感兴趣的:(多线程,职场,同步锁,休闲,syncronized)