public class ThreadTest {
public static void main(String[] args) {
// 守护线程
God god = new God();
You you = new You();
Thread thread = new Thread(god);
thread.setDaemon(true);
// 守护线程启动
thread.start();
// 用户线程
new Thread(you).start();
// System.out.println("==============main退出===============");
}
}
class God implements Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
while (true) {
System.out.println("god protect you");
}
}
}
class You implements Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see Thread#run()
*/
@Override
public void run() {
for (int i = 0; i < 365000; i++) {
System.out.println("you live");
}
System.out.println("===========================我离开==========================");
}
}