File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:
6+ * @date: Created in 2018/12/25
7+ */
8+ public class Daemon {
9+ public static void main (String [] args ) {
10+ Thread thread = new Thread (new DaemonRunner (), "DaemonRunner" );
11+ thread .setDaemon (true );
12+ thread .start ();
13+ }
14+
15+ static class DaemonRunner implements Runnable {
16+ @ Override
17+ public void run () {
18+ try {
19+ SleepUtils .second (10 );
20+ } finally {
21+ System .out .println ("DaemonThread finally run." );
22+ }
23+ }
24+ }
25+ }
Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:
6+ * @date: Created in 2018/12/25
7+ */
8+ public class Interrupted {
9+ public static void main (String [] args ) throws Exception {
10+ //sleepThread不停的尝试睡眠
11+ Thread sleepThread = new Thread (new SleepRunner (), "SleepThread" );
12+ sleepThread .setDaemon (true );
13+ //busyThread不停的运行
14+ Thread busyThread = new Thread (new BusyRunner (), "BusyThread" );
15+ busyThread .setDaemon (true );
16+ sleepThread .start ();
17+ busyThread .start ();
18+ // 休眠5秒,让sleepThread和busyThread充分运行
19+ SleepUtils .second (5 );
20+ sleepThread .interrupt ();
21+ busyThread .interrupt ();
22+ System .out .println ("SleepThread interrupted is " + sleepThread .isInterrupted ());
23+ System .out .println ("BusyThread interrupted is " + busyThread .isInterrupted ());
24+ // 防止sleepThread和busyThread立刻退出
25+ SleepUtils .second (2 );
26+ }
27+
28+ static class SleepRunner implements Runnable {
29+ @ Override
30+ public void run () {
31+ SleepUtils .second (10 );
32+ }
33+ }
34+
35+ static class BusyRunner implements Runnable {
36+ @ Override
37+ public void run () {
38+ while (true ) {
39+
40+ }
41+ }
42+ }
43+ }
Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ import java .lang .management .ManagementFactory ;
4+ import java .lang .management .ThreadInfo ;
5+ import java .lang .management .ThreadMXBean ;
6+
7+ /**
8+ * @author:Gerry
9+ * @description:
10+ * @date: Created in 2018/12/25
11+ */
12+ public class MultiThread {
13+ public static void main (String [] args ) {
14+ // 获取Java线程管理MXBean
15+ ThreadMXBean threadMXBean = ManagementFactory .getThreadMXBean ();
16+ // 不需要获取同步的monitor和synchronizer信息,仅获取线程和线程堆栈信息
17+ ThreadInfo [] threadInfos = threadMXBean .dumpAllThreads (false , false );
18+ // 遍历线程信息,仅打印线程ID和线程名称信息
19+ for (ThreadInfo threadInfo : threadInfos ) {
20+ System .out .println ("[" + threadInfo .getThreadId () + "]" + threadInfo .getThreadName ());
21+ }
22+
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:
6+ * @date: Created in 2018/12/25
7+ */
8+ public class ShutdownThread {
9+ public static void main (String [] args ) {
10+ Runner one = new Runner ();
11+ Thread countThread = new Thread (one , "CountThread" );
12+ countThread .start ();
13+ // 睡眠2秒,main线程对CountThread进行中断,使CountThread能够感知中断而结束
14+ SleepUtils .second (2 );
15+ countThread .interrupt ();
16+ Runner two = new Runner ();
17+ countThread = new Thread (two , "CountThread" );
18+ countThread .start ();
19+ // 睡眠2秒,main线程对Runner two进行取消,使CountThread能够感知on为false而结束
20+ SleepUtils .second (2 );
21+ two .cancle ();
22+ }
23+
24+ public static class Runner implements Runnable {
25+ private long i ;
26+ private volatile boolean on = true ;
27+
28+ @ Override
29+ public void run () {
30+ while (on && !Thread .currentThread ().isInterrupted ()) {
31+ i ++;
32+ }
33+ System .out .println ("Count =" + i );
34+ }
35+
36+ public void cancle () {
37+ on = false ;
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ import java .util .concurrent .TimeUnit ;
4+
5+ /**
6+ * @author:Gerry
7+ * @description:
8+ * @date: Created in 2018/12/25
9+ */
10+ public class SleepUtils {
11+ public static final void second (long seconds ) {
12+ try {
13+ TimeUnit .SECONDS .sleep (seconds );
14+ } catch (InterruptedException e ) {
15+ }
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package ThreadDemo ;
2+
3+ /**
4+ * @author:Gerry
5+ * @description:
6+ * @date: Created in 2018/12/25
7+ */
8+ public class ThreadState {
9+ public static void main (String [] args ) {
10+ new Thread (new TimeWaiting (), "TimeWaitingThread" ).start ();
11+ new Thread (new Waiting (), "WaitingThread" ).start ();
12+ // 使用两个Blocked线程,一个获取锁成功,另一个被阻塞
13+ new Thread (new Blocked (), "BlockedThread-1" ).start ();
14+ new Thread (new Blocked (), "BlockedThread-2" ).start ();
15+ }
16+
17+ //该线程不断的进行睡眠
18+ static class TimeWaiting implements Runnable {
19+ @ Override
20+ public void run () {
21+ while (true ) {
22+ SleepUtils .second (100 );
23+ }
24+ }
25+ }
26+
27+ //该线程在Waiting.Class实例上等待
28+ static class Waiting implements Runnable {
29+ @ Override
30+ public void run () {
31+ while (true ) {
32+ synchronized (Waiting .class ) {
33+ try {
34+ Waiting .class .wait ();
35+ } catch (InterruptedException e ) {
36+ e .printStackTrace ();
37+ }
38+ }
39+ }
40+ }
41+ }
42+
43+ //该线程在Blocked.class实例上加锁后,不会释放该锁
44+ static class Blocked implements Runnable {
45+ @ Override
46+ public void run () {
47+ synchronized (Blocked .class ) {
48+ while (true ) {
49+ SleepUtils .second (100 );
50+ }
51+ }
52+ }
53+ }
54+
55+ }
You can’t perform that action at this time.
0 commit comments