| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example1; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + public class MyService { | ||
| 7 | + synchronized static public void serviveMethod(){ | ||
| 8 | + try { | ||
| 9 | + System.out.println(Thread.currentThread().getName()+" 进入了业务方法"); | ||
| 10 | + Thread.sleep(10000); | ||
| 11 | + } catch (InterruptedException e) { | ||
| 12 | + e.printStackTrace(); | ||
| 13 | + } | ||
| 14 | + } | ||
| 15 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example1; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + /** | ||
| 8 | + * P280 | ||
| 9 | + * 验证NEW,RUNNABLE,TERMINATED | ||
| 10 | + */ | ||
| 11 | + public class Run1_state1 { | ||
| 12 | + public static void main(String[] args) { | ||
| 13 | + try { | ||
| 14 | + Thread1 t = new Thread1(); | ||
| 15 | + System.out.println("main方法中的状态1:"+t.getState()+" of "+t.getName()); | ||
| 16 | + Thread.sleep(1000); | ||
| 17 | + t.start(); | ||
| 18 | + Thread.sleep(1000); | ||
| 19 | + System.out.println("main方法中的状态2:"+t.getState()+" of "+t.getName()); | ||
| 20 | + } catch (InterruptedException e) { | ||
| 21 | + e.printStackTrace(); | ||
| 22 | + } | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + | ||
| 27 | + /* | ||
| 28 | + 输出: | ||
| 29 | + 构造方法中的状态:NEW of Thread-0 | ||
| 30 | + 构造方法中的状态:RUNNABLE of main | ||
| 31 | + main方法中的状态1:NEW of Thread-0 | ||
| 32 | + run方法中的状态:RUNNABLE of Thread-0 | ||
| 33 | + run方法中的状态:RUNNABLE of Thread-0 | ||
| 34 | + main方法中的状态2:TERMINATED of Thread-0 | ||
| 35 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,32 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example1; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + /** | ||
| 8 | + * P282 | ||
| 9 | + * 验证TIMED_WAITING | ||
| 10 | + */ | ||
| 11 | + public class Run1_state2 { | ||
| 12 | + public static void main(String[] args) { | ||
| 13 | + try { | ||
| 14 | + Thread2 t = new Thread2(); | ||
| 15 | + t.start(); | ||
| 16 | + Thread.sleep(1000); | ||
| 17 | + System.out.println("main方法中的状态:"+t.getState()+" of "+t.getName()); | ||
| 18 | + } catch (InterruptedException e) { | ||
| 19 | + e.printStackTrace(); | ||
| 20 | + } | ||
| 21 | + } | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + /* | ||
| 26 | + 输出: | ||
| 27 | + begin sleep,run方法中的状态:RUNNABLE of Thread-0 | ||
| 28 | + begin sleep,run方法中的状态:RUNNABLE of Thread-0 | ||
| 29 | + main方法中的状态:TIMED_WAITING of Thread-0 | ||
| 30 | + end sleep,run方法中的状态:RUNNABLE of Thread-0 | ||
| 31 | + end sleep,run方法中的状态:RUNNABLE of Thread-0 | ||
| 32 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,35 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example1; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + /** | ||
| 8 | + * P283 | ||
| 9 | + * 验证BLOCKED | ||
| 10 | + */ | ||
| 11 | + public class Run1_state3 { | ||
| 12 | + public static void main(String[] args) { | ||
| 13 | + try { | ||
| 14 | + Thread3 t1 = new Thread3(); | ||
| 15 | + t1.setName("a"); | ||
| 16 | + t1.start(); | ||
| 17 | + Thread3 t2 = new Thread3(); | ||
| 18 | + t2.setName("b"); | ||
| 19 | + t2.start(); | ||
| 20 | + Thread.sleep(1000); | ||
| 21 | + System.out.println("main方法中的t2状态:"+t2.getState()); | ||
| 22 | + } catch (InterruptedException e) { | ||
| 23 | + e.printStackTrace(); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + /* | ||
| 31 | + 输出: | ||
| 32 | + a 进入了业务方法 | ||
| 33 | + main方法中的t2状态:BLOCKED | ||
| 34 | + b 进入了业务方法 | ||
| 35 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example1; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + public class Thread1 extends Thread{ | ||
| 7 | + public Thread1() { | ||
| 8 | + System.out.println("构造方法中的状态:"+this.getState()+" of "+this.getName()); | ||
| 9 | + System.out.println("构造方法中的状态:"+Thread.currentThread().getState()+" of "+Thread.currentThread().getName()); | ||
| 10 | + } | ||
| 11 | + | ||
| 12 | + @Override | ||
| 13 | + public void run() { | ||
| 14 | + System.out.println("run方法中的状态:"+this.getState()+" of "+this.getName()); | ||
| 15 | + System.out.println("run方法中的状态:"+Thread.currentThread().getState()+" of "+Thread.currentThread().getName()); | ||
| 16 | + } | ||
| 17 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example1; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + public class Thread2 extends Thread{ | ||
| 7 | + | ||
| 8 | + @Override | ||
| 9 | + public void run() { | ||
| 10 | + try { | ||
| 11 | + System.out.println("begin sleep,run方法中的状态:"+this.getState()+" of "+this.getName()); | ||
| 12 | + System.out.println("begin sleep,run方法中的状态:"+Thread.currentThread().getState()+" of "+Thread.currentThread().getName()); | ||
| 13 | + Thread.sleep(10000); | ||
| 14 | + System.out.println("end sleep,run方法中的状态:"+this.getState()+" of "+this.getName()); | ||
| 15 | + System.out.println("end sleep,run方法中的状态:"+Thread.currentThread().getState()+" of "+Thread.currentThread().getName()); | ||
| 16 | + } catch (InterruptedException e) { | ||
| 17 | + e.printStackTrace(); | ||
| 18 | + } | ||
| 19 | + } | ||
| 20 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example1; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + public class Thread3 extends Thread{ | ||
| 7 | + | ||
| 8 | + @Override | ||
| 9 | + public void run() { | ||
| 10 | + MyService.serviveMethod(); | ||
| 11 | + } | ||
| 12 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,50 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example2; | ||
| 2 | + | ||
| 3 | + import sun.security.provider.SHA; | ||
| 4 | + | ||
| 5 | + /** | ||
| 6 | + * Created by Brian on 2016/4/17. | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | + /** | ||
| 10 | + * 取消System.out.println的注释能看到更多细节 | ||
| 11 | + */ | ||
| 12 | + public class MyThread extends Thread { | ||
| 13 | + private Object lock; | ||
| 14 | + private String showChar; | ||
| 15 | + private int showNumPosition; | ||
| 16 | + private int printCount = 0;//统计打印了几个字母 | ||
| 17 | + volatile private static int addNumber = 1; | ||
| 18 | + | ||
| 19 | + public MyThread(Object lock, String showChar, int showNumPosition) { | ||
| 20 | + this.lock = lock; | ||
| 21 | + this.showChar = showChar; | ||
| 22 | + this.showNumPosition = showNumPosition; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + @Override | ||
| 27 | + public void run() { | ||
| 28 | + try { | ||
| 29 | + synchronized (lock){ | ||
| 30 | + //System.out.println("ThreadName="+ Thread.currentThread().getName()+" get the lock"); | ||
| 31 | + while (true){ | ||
| 32 | + if(addNumber % 3 == showNumPosition){ | ||
| 33 | + System.out.println("ThreadName="+ Thread.currentThread().getName() | ||
| 34 | + +" runCount = "+addNumber+ " "+ showChar); | ||
| 35 | + lock.notifyAll(); | ||
| 36 | + addNumber++; | ||
| 37 | + printCount++; | ||
| 38 | + if(printCount == 3){break;} | ||
| 39 | + }else { | ||
| 40 | + //System.out.println("ThreadName="+ Thread.currentThread().getName()+" will await"); | ||
| 41 | + lock.wait(); | ||
| 42 | + //System.out.println("ThreadName="+ Thread.currentThread().getName()+" after await"); | ||
| 43 | + } | ||
| 44 | + } | ||
| 45 | + } | ||
| 46 | + } catch (InterruptedException e) { | ||
| 47 | + e.printStackTrace(); | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example2; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + /** | ||
| 8 | + * 线程组自动归属特性 | ||
| 9 | + */ | ||
| 10 | + public class Run2_autoAddGroup { | ||
| 11 | + public static void main(String[] args) { | ||
| 12 | + System.out.println("A处线程:"+Thread.currentThread().getName() | ||
| 13 | + +" 所属的线程组名为:"+Thread.currentThread().getThreadGroup().getName() | ||
| 14 | + +" 中有线程组数量:"+ Thread.currentThread().getThreadGroup().activeGroupCount()); | ||
| 15 | + | ||
| 16 | + ThreadGroup group = new ThreadGroup("新的组"); | ||
| 17 | + | ||
| 18 | + System.out.println("A处线程:"+Thread.currentThread().getName() | ||
| 19 | + +" 所属的线程组名为:"+Thread.currentThread().getThreadGroup().getName() | ||
| 20 | + +" 中有线程组数量:"+ Thread.currentThread().getThreadGroup().activeGroupCount()); | ||
| 21 | + | ||
| 22 | + ThreadGroup[] threadGroup = new ThreadGroup[Thread.currentThread().getThreadGroup().activeGroupCount()]; | ||
| 23 | + Thread.currentThread().getThreadGroup().enumerate(threadGroup); | ||
| 24 | + for(int i=0;i<threadGroup.length;i++){ | ||
| 25 | + System.out.println("第一个线程组名称为:"+ threadGroup[i].getName()); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + } | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + /* | ||
| 32 | + 输出: | ||
| 33 | + A处线程:main 所属的线程组名为:main 中有线程组数量:0 | ||
| 34 | + A处线程:main 所属的线程组名为:main 中有线程组数量:1 | ||
| 35 | + 第一个线程组名称为:新的组 | ||
| 36 | + | ||
| 37 | + */ | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + package com.brianway.learning.java.multithread.supplement.example2; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * Created by Brian on 2016/4/17. | ||
| 5 | + */ | ||
| 6 | + | ||
| 7 | + /** | ||
| 8 | + * p288 | ||
| 9 | + * 获取根线程组 | ||
| 10 | + * JVM根线程组就是system | ||
| 11 | + */ | ||
| 12 | + public class Run2_getGroupParent { | ||
| 13 | + public static void main(String[] args) { | ||
| 14 | + System.out.println("线程:"+ Thread.currentThread().getName() | ||
| 15 | + + " 所在的线程组名为:" +Thread.currentThread().getThreadGroup().getName()); | ||
| 16 | + | ||
| 17 | + System.out.println("main线程所在的线程组的父线程组名为:" | ||
| 18 | + +Thread.currentThread().getThreadGroup().getParent().getName()); | ||
| 19 | + | ||
| 20 | + System.out.println("main线程所在的线程组的父线程组的父线程组名为:" | ||
| 21 | + +Thread.currentThread().getThreadGroup().getParent().getParent().getName()); | ||
| 22 | + | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + /* | ||
| 27 | + 输出: | ||
| 28 | + 线程:main 所在的线程组名为:main | ||
| 29 | + main线程所在的线程组的父线程组名为:system | ||
| 30 | + Exception in thread "main" java.lang.NullPointerException | ||
| 31 | + at com.brianway.learning.java.multithread.supplement.example2.Run2_getGroupParent.main(Run2_getGroupParent.java:20) | ||
| 32 | + at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) | ||
| 33 | + at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) | ||
| 34 | + at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) | ||
| 35 | + at java.lang.reflect.Method.invoke(Method.java:483) | ||
| 36 | + at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) | ||
| 37 | + */ | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments