标签:
1 package concurrent;
2
3 class MyThread1 extends Thread {
4 public void run() {
5 try {
6 for (int i = 0; i < 500000; i++) {
7 if (this.isInterrupted()) {
8 System.out.println("本线程被中断了:马上退出");
9 throw new InterruptedException();
10 }
11 System.out.println("i=" + (i + 1));
12 }
13 System.out.println("for循环外的,线程继续运行,并没有立即被中断!");
14 } catch (InterruptedException e) {
15 System.out.println("线程被中断后,如果有catch会进入异常处理,之后就不会继续往下运行!");
16 e.printStackTrace();
17 }
18 }
19 }
20 public class Testinterrupted {
21 public static void main(String[] args) {
22 try {
23 MyThread1 thread = new MyThread1();
24 thread.start();
25 System.out.println(Thread.currentThread().getName());
26 Thread.sleep(1000);// 把主线程停1秒
27 // thread.interrupt();//中断当前线程操作
28 Thread.currentThread().interrupt();// 中断当前线程操作
29 System.out.println("测试当前线程是否已经停止1:" + Thread.interrupted());
30 System.out.println("测试当前线程是否已经停止2:" + Thread.interrupted());
31 thread.interrupt();
32 System.out.println("测试指定对象线程是否已经停止1:" + thread.isInterrupted());
33 System.out.println("测试指定对象线程是否已经停止2:" + thread.isInterrupted());
34 } catch (InterruptedException e) {
35 e.printStackTrace();
36 }
37 System.out.println("end!");
38 }
39 }
1 package concurrent;
2
3 class ThreadA extends Thread {
4 private int count = 0;
5 public int getCount(){
6 return count;
7 }
8 public void run() {
9 try {
10 for (int i = 0; i < 1000000; i++) {
11 count = i;
12 System.out.println(this.getName()+"-------->"+count);
13 if (count >= 5000) {
14 throw new InterruptedException();
15 }
16 }
17 } catch (InterruptedException e) {
18 System.out.println(this.getName() + " 线程已经执行完毕!");
19 }
20 }
21 }
22
23 class ThreadB extends Thread {
24 private int count = 0;
25 public int getCount(){
26 return count;
27 }
28 public void run() {
29 try {
30 for (int i = 0; i < 1000000; i++) {
31 count = i;
32 System.out.println(this.getName()+"-->"+count);
33 if (count >= 5000) {
34 throw new InterruptedException();
35 }
36 }
37 } catch (InterruptedException e) {
38 System.out.println(this.getName() + " 线程已经执行完毕!");
39 }
40 }
41 }
42
43 class MyThread2 extends Thread {
44
45 public void run() {
46 long beginTime = System.currentTimeMillis();
47 int count = 0;
48 for (int i = 0; i < 10000000; i++) {
49 // Thread.yield();
50 // 放弃当前的CPU资源的执行权,将它让给其他的任务去占用CPU的执行时间,时间不确定,可能又马上获取到了执行权继续执行;
51 count = count + (i + 1) * 2 / 2;
52 }
53 long endTime = System.currentTimeMillis();
54 System.out.println(this.getName() + "--"
55 + Thread.currentThread().getPriority());
56 System.out.println(this.getPriority());
57 System.out.println(endTime - beginTime);
58 }
59 }
60
61 public class TestYield {
62 public static void main(String[] args) {
63 // MyThread2 m = new MyThread2();
64 // m.start();
65 // System.out.println(Thread.currentThread().getName());
66 // System.out.println(Thread.currentThread().getPriority());
67
68 try {
69 ThreadA a = new ThreadA();
70 ThreadB b = new ThreadB();
71 a.setPriority(Thread.MAX_PRIORITY);
72 b.setPriority(Thread.NORM_PRIORITY - 2);
73 a.start();
74 b.start();
75 Thread.sleep(20000);
76 } catch (InterruptedException e) {
77 e.printStackTrace();
78 }
79 }
80 }
1 package concurrent; 2 3 class ThreadDaemon extends Thread { 4 private int i = 0; 5 public void run() { 6 try { 7 while (true) { 8 i++; 9 System.out.println(this.getName() + "-->" + i); 10 Thread.sleep(1000); 11 /*设置为守护线程后,线程sleep后会自动停止,这样可以当作一种线程停止的手段*/ 12 } 13 } catch (InterruptedException e) { 14 e.printStackTrace(); 15 } 16 } 17 } 18 public class TestDaemon { 19 public static void main(String[] args) { 20 try { 21 ThreadDaemon td = new ThreadDaemon(); 22 td.setDaemon(true); 23 td.start(); 24 Thread.sleep(1000); 25 System.out.println("线程td先设置为守护线程,运行后sleep后即停止了!"); 26 } catch (InterruptedException e) { 27 e.printStackTrace(); 28 } 29 } 30 }
标签:
原文地址:http://www.cnblogs.com/lmy-foolishbird/p/5469163.html