我相信主线程不能在子线程之前死掉.但有什么方法可以检查吗?我在下面写了一个简单的程序.任何人都可以证明它几乎将理论抛在一边吗?
class childre extends Thread
{
public void run()
{
for( int i=0 ; i<10 ;i++)
{
System.out.println( " child " + i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class ChildThreadb4main
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println("main");
childre c1 = new childre();
c1.start();
for(int i=0;i<5;i++)
{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// …Run Code Online (Sandbox Code Playgroud) 我在主方法中启动了 t1 线程并想停止主线程但我的 t1 线程仍在运行。有可能的?如何?
public static void main(String[] args)
{
Thread t1=new Thread()
{
public void run()
{
while(true)
{
try
{
Thread.sleep(2000);
System.out.println("thread 1");
}
catch(Exception e)
{}
}
}
};
t1.start();
}
Run Code Online (Sandbox Code Playgroud)