| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,11 +8,6 @@ | |||
| 8 | 8 | ||
| 9 | 9 | 不定时进行调整和补充,需要关注更新的请 Watch、Star、Fork | |
| 10 | 10 | ||
| 11 | - 如果你只是单纯要阅读的话,建议移步CSDN或者oschina上观看,访问速度快很多: | ||
| 12 | - | ||
| 13 | - >* CSDN:[我的java&javaweb学习笔记(汇总)](http://blog.csdn.net/h3243212/article/details/50659471) | ||
| 14 | - >* oschina:[我的java&javaweb学习笔记(汇总)](http://my.oschina.net/brianway/blog/614355) | ||
| 15 | - | ||
| 16 | 11 | ||
| 17 | 12 | ----- | |
| 18 | 13 | ||
@@ -26,6 +21,12 @@ | |||
| 26 | 21 | ||
| 27 | 22 | # 博客文档 | |
| 28 | 23 | ||
| 24 | + 如果你只是单纯要阅读的话,建议移步CSDN或者oschina上观看,访问速度快很多: | ||
| 25 | + | ||
| 26 | + >* CSDN:[我的java&javaweb学习笔记(汇总)](http://blog.csdn.net/h3243212/article/details/50659471) | ||
| 27 | + >* oschina:[我的java&javaweb学习笔记(汇总)](http://my.oschina.net/brianway/blog/614355) | ||
| 28 | + | ||
| 29 | + | ||
| 29 | 30 | **博客目录** | |
| 30 | 31 | ||
| 31 | 32 | - [javase](/blogs/javase) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,97 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + import org.omg.CORBA.PUBLIC_MEMBER; | ||
| 8 | + | ||
| 9 | + /** | ||
| 10 | + * P101 | ||
| 11 | + * 验证同步sychronized(class)代码块的作用 | ||
| 12 | + * 顺便验证了下static方法是不能复写的 | ||
| 13 | + * @see ServiceSub,ServiceSub2 | ||
| 14 | + */ | ||
| 15 | + public class Run10_synBlockMoreObjectOneLock { | ||
| 16 | + public static void main(String[] args) { | ||
| 17 | + //testSub(); | ||
| 18 | + //testSub2(); | ||
| 19 | + testBlock(); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 验证同步sychronized(class)代码块的作用 | ||
| 24 | + */ | ||
| 25 | + public static void testBlock(){ | ||
| 26 | + final ServiceSub service1 = new ServiceSub(); | ||
| 27 | + final ServiceSub service2 = new ServiceSub(); | ||
| 28 | + | ||
| 29 | + Thread a = new Thread(){ | ||
| 30 | + @Override | ||
| 31 | + public void run() { | ||
| 32 | + service1.printA(); | ||
| 33 | + } | ||
| 34 | + }; | ||
| 35 | + a.setName("A"); | ||
| 36 | + a.start(); | ||
| 37 | + Thread b = new Thread(){ | ||
| 38 | + @Override | ||
| 39 | + public void run() { | ||
| 40 | + service2.printB(); | ||
| 41 | + } | ||
| 42 | + }; | ||
| 43 | + b.setName("B"); | ||
| 44 | + b.start(); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * testSub断点调试结果是运行的Service类的printA方法 | ||
| 49 | + */ | ||
| 50 | + public static void testSub(){ | ||
| 51 | + ServiceSub service1 = new ServiceSub(); | ||
| 52 | + ServiceSub service2 = new ServiceSub(); | ||
| 53 | + | ||
| 54 | + ThreadA a = new ThreadA(service1); | ||
| 55 | + a.setName("A"); | ||
| 56 | + a.start(); | ||
| 57 | + ThreadB b = new ThreadB(service2); | ||
| 58 | + b.setName("B"); | ||
| 59 | + b.start(); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * testSub2断点调试结果是运行的Service类的printA方法 | ||
| 64 | + */ | ||
| 65 | + public static void testSub2(){ | ||
| 66 | + ServiceSub2 service1 = new ServiceSub2(); | ||
| 67 | + ServiceSub2 service2 = new ServiceSub2(); | ||
| 68 | + | ||
| 69 | + ThreadA a = new ThreadA(service1); | ||
| 70 | + a.setName("A"); | ||
| 71 | + a.start(); | ||
| 72 | + ThreadB b = new ThreadB(service2); | ||
| 73 | + b.setName("B"); | ||
| 74 | + b.start(); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + | ||
| 81 | + /* | ||
| 82 | + 输出: | ||
| 83 | + In ServiceSub | ||
| 84 | + 线程名:A 在 1460480421944进入printA | ||
| 85 | + 线程名:A 在 1460480423944离开printA | ||
| 86 | + 线程名:B 在 1460480423944进入printB | ||
| 87 | + 线程名:B 在 1460480423944离开printB | ||
| 88 | + | ||
| 89 | + --------- | ||
| 90 | + 也可能: | ||
| 91 | + 线程名:B 在 1460480476089进入printB | ||
| 92 | + 线程名:B 在 1460480476089离开printB | ||
| 93 | + In ServiceSub | ||
| 94 | + 线程名:A 在 1460480476089进入printA | ||
| 95 | + 线程名:A 在 1460480478089离开printA | ||
| 96 | + | ||
| 97 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + /** | ||
| 8 | + * P100 | ||
| 9 | + * Class锁可以对类的所有对象实例起作用 | ||
| 10 | + */ | ||
| 11 | + public class Run10_synMoreObjectStaticOneLock { | ||
| 12 | + public static void main(String[] args) { | ||
| 13 | + Service service1 = new Service(); | ||
| 14 | + Service service2 = new Service(); | ||
| 15 | + | ||
| 16 | + ThreadA a = new ThreadA(service1); | ||
| 17 | + a.setName("A"); | ||
| 18 | + a.start(); | ||
| 19 | + ThreadB b = new ThreadB(service2); | ||
| 20 | + b.setName("B"); | ||
| 21 | + b.start(); | ||
| 22 | + | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + /* | ||
| 27 | + 输出: | ||
| 28 | + 线程名:A 在 1460479094611进入printA | ||
| 29 | + 线程名:A 在 1460479097611离开printA | ||
| 30 | + 线程名:B 在 1460479097611进入printB | ||
| 31 | + 线程名:B 在 1460479097611离开printB | ||
| 32 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + | ||
| 8 | + /** | ||
| 9 | + * P97 | ||
| 10 | + * 验证synchronized关键字加载static静态方法和加到非静态方法不是同一个锁 | ||
| 11 | + * 一个是Class类上锁,一个是对象上锁 | ||
| 12 | + */ | ||
| 13 | + public class Run10_synTwoLock { | ||
| 14 | + public static void main(String[] args) { | ||
| 15 | + Service service = new Service(); | ||
| 16 | + | ||
| 17 | + ThreadA a = new ThreadA(service); | ||
| 18 | + a.setName("A"); | ||
| 19 | + a.start(); | ||
| 20 | + ThreadB b = new ThreadB(service); | ||
| 21 | + b.setName("B"); | ||
| 22 | + b.start(); | ||
| 23 | + ThreadC c =new ThreadC(service); | ||
| 24 | + c.setName("C"); | ||
| 25 | + c.start(); | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + /* | ||
| 30 | + 输出: | ||
| 31 | + 线程名:A 在 1460478479803进入printA | ||
| 32 | + 线程名:C 在 1460478479808进入printC | ||
| 33 | + 线程名:C 在 1460478479808离开printC | ||
| 34 | + 线程名:A 在 1460478482804离开printA | ||
| 35 | + 线程名:B 在 1460478482804进入printB | ||
| 36 | + 线程名:B 在 1460478482804离开printB | ||
| 37 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + public class Service { | ||
| 8 | + synchronized public static void printA(){ | ||
| 9 | + try { | ||
| 10 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"进入printA"); | ||
| 11 | + Thread.sleep(3000); | ||
| 12 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"离开printA"); | ||
| 13 | + } catch (InterruptedException e) { | ||
| 14 | + e.printStackTrace(); | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + synchronized public static void printB(){ | ||
| 19 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"进入printB"); | ||
| 20 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"离开printB"); | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + synchronized public void printC(){ | ||
| 24 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"进入printC"); | ||
| 25 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"离开printC"); | ||
| 26 | + } | ||
| 27 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,30 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + public class ServiceSub extends Service { | ||
| 7 | + | ||
| 8 | + public static void printA(){ | ||
| 9 | + synchronized (ServiceSub.class){ | ||
| 10 | + try { | ||
| 11 | + System.out.println("In ServiceSub"); | ||
| 12 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"进入printA"); | ||
| 13 | + Thread.sleep(2000); | ||
| 14 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"离开printA"); | ||
| 15 | + } catch (InterruptedException e) { | ||
| 16 | + e.printStackTrace(); | ||
| 17 | + } | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public static void printB(){ | ||
| 22 | + synchronized (ServiceSub.class){ | ||
| 23 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"进入printB"); | ||
| 24 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"离开printB"); | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,22 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + public class ServiceSub2 extends Service{ | ||
| 7 | + synchronized public static void printA(){ | ||
| 8 | + try { | ||
| 9 | + System.out.println("ServiceSub2"); | ||
| 10 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"进入printA"); | ||
| 11 | + Thread.sleep(1000); | ||
| 12 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"离开printA"); | ||
| 13 | + } catch (InterruptedException e) { | ||
| 14 | + e.printStackTrace(); | ||
| 15 | + } | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + synchronized public static void printB(){ | ||
| 19 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"进入printB"); | ||
| 20 | + System.out.println("线程名:"+Thread.currentThread().getName()+" 在 "+ System.currentTimeMillis()+"离开printB"); | ||
| 21 | + } | ||
| 22 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + public class ThreadA extends Thread { | ||
| 7 | + private Service service; | ||
| 8 | + | ||
| 9 | + public ThreadA(Service service) { | ||
| 10 | + super(); | ||
| 11 | + this.service = service; | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + @Override | ||
| 15 | + public void run() { | ||
| 16 | + service.printA(); | ||
| 17 | + } | ||
| 18 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + public class ThreadB extends Thread { | ||
| 7 | + private Service service; | ||
| 8 | + | ||
| 9 | + public ThreadB(Service service) { | ||
| 10 | + super(); | ||
| 11 | + this.service = service; | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + @Override | ||
| 15 | + public void run() { | ||
| 16 | + service.printB(); | ||
| 17 | + } | ||
| 18 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.synchronize.exmaple10; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by brian on 2016/4/13. | ||
| 5 | + */ | ||
| 6 | + public class ThreadC extends Thread { | ||
| 7 | + private Service service; | ||
| 8 | + | ||
| 9 | + public ThreadC(Service service) { | ||
| 10 | + super(); | ||
| 11 | + this.service = service; | ||
| 12 | + } | ||
| 13 | + | ||
| 14 | + @Override | ||
| 15 | + public void run() { | ||
| 16 | + service.printC(); | ||
| 17 | + } | ||
| 18 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments