Java 线程 synchronized 学习
2016-09-22 15:03
288 查看
**synchronized** 关键字:当一个方法被synchronized关键字修饰的时候,该方法叫同步方法 Java中的每个对象都有一个锁(lock)或者叫监视器(monitor),当访问某个线程的访问synchronized 方法时,表示给该对象上锁,此时其他任何线程都无法访问该synchronized 方法,直到之前的那个线程执行完方法或者抛出异常,那个该对象的锁被释放掉,其他线程才可以访问该synchronized 方法 public class ThreadTest { public static void main(String[] args) { Example example = new Example(); TheThread t1 = new TheThread(example); TheThread2 t2 = new TheThread2(example); t1.start(); t2.start(); } } class Example { public synchronized void execute() { for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("hello: " + i + " "); } } public synchronized void execute2() { for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("word: " + i + " "); } } } class TheThread extends Thread { private Example example; public TheThread(Example example) { this.example = example; } @Override public void run() { super.run(); example.execute(); } } class TheThread2 extends Thread { private Example example; public TheThread2(Example example) { this.example = example; } @Override public void run() { super.run(); example.execute2(); } } 打印的顺序是 hello: 0 hello: 1 hello: 2 hello: 3 hello: 4 hello: 5 hello: 6 hello: 7 hello: 8 hello: 9 word: 0 word: 1 word: 2 word: 3 word: 4 word: 5 word: 6 word: 7 word: 8 word: 9 从结果上看是有序的,也就是说如果一个对象有多个synchronized方法,某一个时刻某个线程已经进入某个synchronized 方法,那么该方法没有执行完之前,其他线程无法访问该对象的任何synchronized 方法,当然非synchronized 还是可以访问的。 将execute2方法改成static的 public synchronized static void execute2() 打印的结果如下: word: 0 hello: 0 hello: 1 word: 1 word: 2 hello: 2 word: 3 hello: 3 hello: 4 word: 4 hello: 5 word: 5 hello: 6 word: 6 原因分析:static修饰的方法或者变量不属于任何对象,属于类的,当synchronized 修饰一 个static方法时,锁的不是当前对象,而是当前对象所对应的class对象,而一个类不管有多少对象,其所对应的class对象只有一个。这个时候把execute方法也改成static,打印结果如下: hello: 0 hello: 1 hello: 2 hello: 3 hello: 4 hello: 5 hello: 6 hello: 7 hello: 8 hello: 9 word: 0 word: 1 word: 2 word: 3 word: 4 word: 5 word: 6 word: 7 word: 8 word: 9 再次验证,如果某个synchronized 方法是static的,当线程访问该方法时,它锁的并不是synchronized 方法所处在的对象,而是这个类的Class对象,而Java一个类的对象无论有多少个,Class对象始终只有一个,因此当多个线程访问同一个类多个对象的不同synchronized static方法时,执行的结果也是按照顺序的,也就是一个线程执行完毕后另外一个线程才能执行。
相关文章推荐
- Java学习之线程锁--synchronized
- java 语言多线程可见性(synchronized 和 volatile 学习)
- Java学习之线程锁--单例模式写法--synchronized
- Java线程入门学习5----volatile和synchronized关键字
- java学习12--线程的可见性,synchronized,volatile使用
- java学习初探十八之线程的线程同步_锁机制_synchronized
- Java学习之线程锁--单例模式--synchronized
- Java学习之线程锁--单例模式写法--synchronized
- JAVA基础学习--一个线程加一运算,一个线程做减一运算,多个线程同时交替运行--synchronized
- java并发编程学习:如何等待多个线程执行完成后再继续后续处理(synchronized、join、FutureTask、CyclicBarrier)
- JAVA线程基础(synchronized锁的学习)(一)
- 【Java学习】线程的同步-synchronized的理解
- java并发编程学习:如何等待多个线程执行完成后再继续后续处理(synchronized、join、FutureTask、CyclicBarrier)
- java多线程学习一线程安全之内存、synchronized、volatile
- java学习——线程启动的两种方法,Time定时器,synchronized 的使用
- Java线程学习11.30
- 使用synchronized进行Java线程同步
- java线程初步学习
- Java线程同步机制synchronized关键字的理解
- java线程等待、设置优先级、同步、挂起、恢复(Join、setPriority、synchronized、wait、notify……)