synchronized用于多线程设计,有了synchronized关键字,多线程程序的运行结果将变得能够控制。synchronized关键字用于保护共享数据。多线程
synchronized实现同步的机制:synchronized依靠"锁"机制进行多线程同步,"锁"有2种,一种是对象锁,一种是类锁。ide
初始化一个对象时,自动有一个对象锁。synchronized {普通方法}依靠对象锁工做,多线程访问synchronized方法,一旦某个进程抢得锁以后,其余的进程只有排队对待。函数
public class TestSynchronized { public synchronized void method1() throws InterruptedException { System.out.println("method1 begin at:" + System.currentTimeMillis()); Thread.sleep(6000); System.out.println("method1 end at:" + System.currentTimeMillis()); } public synchronized void method2() throws InterruptedException { while(true) { System.out.println("method2 running"); Thread.sleep(200); } } static TestSychronized instance = new TestSychronized(); public static void main(String[] args) { Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { instance.method1(); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=1; i<4; i++) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread1 still alive"); } } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { instance.method2(); } catch (InterruptedException e) { e.printStackTrace(); } } }); thread1.start(); thread2.start(); } }
运行结果:thread2一直等到thread1中的method1执行完了以后才执行method2,说明method1和method2互斥this
method1 begin at:1381584063557 method1 end at:1381584069557 method2 running method2 running Thread1 still alive method2 running Thread1 still alive method2 running Thread1 still alive method2 running method2 running method2 running method2 running method2 running method2 running method2 running method2 running
package com.free4lab.lol; public class TestSychronized { public synchronized static void method1() throws InterruptedException { System.out.println("method1 begin at:" + System.currentTimeMillis()); Thread.sleep(6000); System.out.println("method1 end at:" + System.currentTimeMillis()); } public synchronized static void method2() throws InterruptedException { while(true) { System.out.println("method2 running"); Thread.sleep(200); } } static TestSychronized instance1 = new TestSychronized(); static TestSychronized instance2 = new TestSychronized(); public static void main(String[] args) { Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { instance1.method1(); } catch (InterruptedException e) { e.printStackTrace(); } for(int i=1; i<4; i++) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread1 still alive"); } } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { instance2.method2(); } catch (InterruptedException e) { e.printStackTrace(); } } }); thread1.start(); thread2.start(); } }
输出效果也是method1和method2互斥spa
package com.free4lab.lol; public class TestSychronized { static TestSychronized instance = new TestSychronized(); public static void main(String[] args) { Thread thread1 = new Thread(new Runnable() { @Override public synchronized void run() { for(int i=1; i<4; i++) { try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Thread1 still alive, " + i); } } }); new Thread(thread1).start(); new Thread(thread1).start(); } }
若是加了synchronized当前线程取完全部数据后,才会释放锁,输出结果是有序的:线程
Thread1 still alive, 1 Thread1 still alive, 2 Thread1 still alive, 3 Thread1 still alive, 1 Thread1 still alive, 2 Thread1 still alive, 3