线程基础(四)
CurrentThreadTest
package org.wp.thread;/** * * activeCount() * 返回当前线程的线程组中活动线程的数目 * enumerate(Thread[] tarray) * 将当前线程的线程组及其子组中的每一个活动线程复制到指定的数组中 * * @author wp * */public class CurrentThreadTest {public static void main(String args[]) throws Exception {Thread t0 = new ThreadZero();Thread t1 = new ThreadOne();Thread t2 = new ThreadTwo();Thread t3 = new ThreadThree();t0.start();t1.start();Thread.sleep(6000);t2.setDaemon(true);t2.start();Thread.sleep(6000);t3.start();}}class MyThreadViewer {public static void view() {int total = Thread.activeCount();System.out.println("当前线程名称:" + Thread.currentThread().getName());System.out.println("活动线程总数:" + total + "个");Thread[] threads = new Thread[total];Thread.enumerate(threads);for (Thread thread : threads) {String role = thread.isDaemon() ? "后台线程" : "用户线程";System.out.println(role + thread.getName());}System.out.println("---------------------------------------------");System.out.println();}}class ThreadZero extends Thread {@Overridepublic void run() {try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}}}class ThreadOne extends Thread {@Overridepublic void run() {MyThreadViewer.view();}}class ThreadTwo extends Thread {@Overridepublic void run() {MyThreadViewer.view();}}class ThreadThree extends Thread {@Overridepublic void run() {MyThreadViewer.view();}}?DaemonThreadTest
package org.wp.thread;/** * * setDaemon(boolean on) * 将该线程标记为守护线程或用户线程 * 当正在运行的线程都是守护线程时,Java虚拟机退出 * 该方法必须在启动线程前调用 * * @author wp * */public class DaemonThreadTest {public static void main(String args[]) {Thread t1 = new MyThread(10);t1.setName("用户线程T1");t1.start();// 由于Java虚拟机退出,无法正常执行完成Thread t2 = new MyThread(1000);t2.setDaemon(true);t2.setName("后台线程T2");t2.start();for (int i = 0; i < 10; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}System.out.println(Thread.currentThread().getName() + "结束!");}}class MyThread extends Thread {private int num;public MyThread(int num) {this.num = num;}@Overridepublic void run() {for (int i = 0; i < num; i++) {System.out.println(this.getName() + ":" + i);}System.out.println(this.getName() + "结束!");}}?JoinTest
package org.wp.thread;public class JoinTest {public static void main(String args[]) {JoinThread jt = new JoinThread();jt.start();try {jt.join(10);} catch (InterruptedException e) {e.printStackTrace();}for (int i = 0; i < 50; i++) {System.out.println(Thread.currentThread().getName() + ":" + i);}}}class JoinThread extends Thread {@Overridepublic void run() {for (int i = 0; i < 500; i++) {System.out.println(this.getName() + ":" + i);}}}?YieldTest
package org.wp.thread;import java.util.Date;public class YieldTest {public static void main(String args[]) {new YieldThread(false).start();new YieldThread(true).start();new YieldThread(false).start();}}class YieldThread extends Thread {private boolean flag;public YieldThread(boolean flag) {this.flag = flag;}@Overridepublic void run() {long start = new Date().getTime();for (int i = 0; i < 500; i++) {if (flag) {Thread.yield();}System.out.println(this.getName() + ": " + i);}long end = new Date().getTime();System.out.println("\n" + this.getName() + "执行时间:" + (end - start)+ "毫秒");}}?DeadLockTest
package org.wp.thread;public class DeadLockTest {public static void main(String args[]) {char[] a = { 'A', 'B', 'C' };char[] b = { 'C', 'D', 'E' };new TestThread(a, b).start();new TestThread(b, a).start();}}class TestThread extends Thread {private char[] source;private char[] dest;public TestThread(char[] source, char[] dest) {this.source = source;this.dest = dest;}@Overridepublic void run() {synchronized (source) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}synchronized (dest) {System.arraycopy(source, 0, dest, 0, source.length);System.out.println(dest);}}}}?TestDeadLock
package org.wp.thread;public class TestDeadLock {public static void main(String args[]) {StringBuffer sb = new StringBuffer("ABC");Thread tt = new ThreadTest(sb);tt.start();synchronized (sb) {try {tt.join();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(sb);}System.out.println(Thread.currentThread().getName() + "结束!");}}class ThreadTest extends Thread {private StringBuffer sb;public ThreadTest(StringBuffer sb) {this.sb = sb;}@Overridepublic void run() {synchronized (sb) {sb.reverse();}System.out.println(this.getName() + "结束!");}}