首页 > 其他 > 详细

Thread类

时间:2017-01-23 23:52:41      阅读:263      评论:0      收藏:0      [点我收藏+]

 

public final void join()

throws InterruptedException

 

/*
 * public final void join()
 * throws InterruptedException
 * 
 * 等待该线程终止。
 * */

public class IntegerDemo {
	public static void main(String[] args) {

		myThread my1 = new myThread();
		myThread my2 = new myThread();
		myThread my3 = new myThread();

		my1.setName("hello");
		my2.setName("world");
		my3.setName("java");

		my1.start();

		try {
			my1.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		my2.start();
		my3.start();
	}
}

class myThread extends Thread {
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.println(getName() + ":" + i);
		}

		System.out.println(Thread.currentThread().getName());
	}
}

 

public final void setDaemon(boolean on)

 

/*
 * public final void setDaemon(boolean on)
 * 
 * 将该线程标记为守护线程或用户线程。当正在运行的线程都是守护线程时,Java虚拟机退出。
 * 该方法必须在启动线程前调用。
 * */

public class IntegerDemo {
	public static void main(String[] args) {

		myThread my1 = new myThread();
		myThread my2 = new myThread();
		myThread my3 = new myThread();

		my1.setName("hello");
		my2.setName("world");
		my3.setName("java");

		my1.setDaemon(true);

		my1.start();
		my2.start();
		my3.start();
	}
}

class myThread extends Thread {
	public void run() {
		for (int i = 0; i < 100; i++) {
			System.out.println(getName() + ":" + i);
		}

		System.out.println(Thread.currentThread().getName());
	}
}

 

Thread类

原文:http://www.cnblogs.com/denggelin/p/6345133.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!