相关疑难解决方法(0)

主线程可以在子线程之前死掉

我相信主线程不能在子线程之前死掉.但有什么方法可以检查吗?我在下面写了一个简单的程序.任何人都可以证明它几乎将理论抛在一边吗?

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)

java multithreading

6
推荐指数
2
解决办法
1万
查看次数

如何在另一个线程仍在运行时停止主线程

我在主方法中启动了 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)

java multithreading

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

java ×2

multithreading ×2