|
| 1 | +package io.github.dunwu.javase.concurrent.chapter01; |
| 2 | + |
| 3 | +import org.junit.Assert; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author Zhang Peng |
| 8 | + */ |
| 9 | +public class ThreadDemoTest extends Thread { |
| 10 | + @Test |
| 11 | + public void test() { |
| 12 | + ThreadDemo threadDemo1 = new ThreadDemo("线程A"); // 实例化对象 |
| 13 | + ThreadDemo threadDemo2 = new ThreadDemo("线程B"); // 实例化对象 |
| 14 | + ThreadDemo threadDemo3 = new ThreadDemo("线程C"); // 实例化对象 |
| 15 | + threadDemo1.run(); // 调用线程主体 |
| 16 | + threadDemo2.run(); // 调用线程主体 |
| 17 | + threadDemo3.run(); // 调用线程主体 |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testCurrentThread() { |
| 22 | + MyThread target = new MyThread(); // 实例化Runnable子类对象 |
| 23 | + new Thread(target, "线程").start(); // 启动线程 |
| 24 | + target.run(); // 直接调用run()方法 |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testSetDaemon() { |
| 29 | + MyThread target = new MyThread(); // 实例化Runnable子类对象 |
| 30 | + Thread t = new Thread(target, "线程"); // 实例化Thread对象 |
| 31 | + t.setDaemon(true); // 此线程在后台运行 |
| 32 | + t.start(); // 启动线程 |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testThreadName() { |
| 37 | + MyThread target = new MyThread(); // 实例化Runnable子类对象 |
| 38 | + new Thread(target).start(); // 系统自动设置线程名称 |
| 39 | + new Thread(target, "线程-A").start(); // 手工设置线程名称 |
| 40 | + new Thread(target, "线程-B").start(); // 手工设置线程名称 |
| 41 | + new Thread(target).start(); // 系统自动设置线程名称 |
| 42 | + new Thread(target).start(); // 系统自动设置线程名称 |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testThreadAlive() { |
| 47 | + MyThread mt = new MyThread(); // 实例化Runnable子类对象 |
| 48 | + Thread t = new Thread(mt, "线程"); // 实例化Thread对象 |
| 49 | + System.out.println("线程开始执行之前 --> " + t.isAlive()); // 判断是否启动 |
| 50 | + Assert.assertFalse(t.isAlive()); |
| 51 | + t.start(); // 启动线程 |
| 52 | + System.out.println("线程开始执行之后 --> " + t.isAlive()); // 判断是否启动 |
| 53 | + Assert.assertTrue(t.isAlive()); |
| 54 | + for (int i = 0; i < 3; i++) { |
| 55 | + System.out.println(" main运行 --> " + i); |
| 56 | + } |
| 57 | + // 以下的输出结果不确定 |
| 58 | + System.out.println("代码执行之后 --> " + t.isAlive()); // 判断是否启动 |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testThreadPriority() { |
| 63 | + Thread t1 = new Thread(new MyThread(), "线程A"); // 实例化线程对象 |
| 64 | + Thread t2 = new Thread(new MyThread(), "线程B"); // 实例化线程对象 |
| 65 | + Thread t3 = new Thread(new MyThread(), "线程C"); // 实例化线程对象 |
| 66 | + t1.setPriority(Thread.MIN_PRIORITY); // 优先级最低 |
| 67 | + t2.setPriority(Thread.MAX_PRIORITY); // 优先级最低 |
| 68 | + t3.setPriority(Thread.NORM_PRIORITY); // 优先级最低 |
| 69 | + t1.start(); // 启动线程 |
| 70 | + t2.start(); // 启动线程 |
| 71 | + t3.start(); // 启动线程 |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void testThreadSleep() { |
| 76 | + MyThread target = new MyThread(); // 实例化Runnable子类对象 |
| 77 | + Thread t = new Thread(target, "线程"); // 实例化Thread对象 |
| 78 | + t.start(); // 启动线程 |
| 79 | + |
| 80 | + } |
| 81 | +} |
0 commit comments