Java 通过扩展线程类创建多个线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19130672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:22:11  来源:igfitidea点击:

Creating multiple threads by extending thread class

javamultithreading

提问by Aadithya

I want to know how multiple threads can be created by "extending the Thread class". I know how it is done using "Runnable". Please tell me how it can be done by "extending Thread class".

我想知道如何通过“扩展 Thread 类”来创建多个线程。我知道它是如何使用“Runnable”完成的。请告诉我如何通过“扩展 Thread 类”来完成。

采纳答案by Digital Alchemist

Main Method

主要方法

public class ThreadDemo 
{   

  public static void main(String args[])    
  {         

    //Creating an object of the first thread        
    FirstThread   firstThread = new FirstThread();              

    //Creating an object of the Second thread       
    SecondThread   secondThread = new SecondThread();               

    //Starting the first thread         
    firstThread.start();                

    //Starting the second thread        
    secondThread.start();   
  } 
}

This class is made as a thread by extending "Thread" class.

这个类是通过扩展“Thread”类作为线程的。

public class FirstThread extends Thread 
{ 
  //This method will be executed when this thread is executed 
  public void run() 
  { 

    //Looping from 1 to 10 to display numbers from 1 to 10 
     for (int i=1; i<=10; i++) 
     { 
        //Displaying the numbers from this thread 
        System.out.println( "Messag from First Thread : " +i); 

        /*taking a delay of one second before displaying next number 
        * 
        * "Thread.sleep(1000);" - when this  statement is executed, 
        * this thread will sleep for 1000  milliseconds (1 second) 
        * before executing the next statement. 
        * 
        * Since  we are making this thread to sleep for one second, 
        * we need to handle  "InterruptedException". Our thread 
        * may throw this exception if it is  interrupted while it 
        * is sleeping. 
        * 
        */
        try
        { 
           Thread.sleep(1000); 
        } 
        catch (InterruptedException  interruptedException) 
        { 
          /*Interrupted exception will be thrown when a sleeping or  waiting 
          * thread is interrupted. 
          */
          System.out.println(  "First Thread is interrupted when it is  sleeping" +interruptedException); 
        } 
      } 
   }

This class is made as a thread by extending "Thread" class.

这个类是通过扩展“Thread”类作为线程的。

public class SecondThread extends Thread 
{   

  //This method will be executed when this thread is executed   
  public void run()     
  {             
    //Looping from 1 to 10 to display numbers from 1 to 10      
    for (int i=1; i<=10; i++)        
    {           
       System.out.println( "Messag from Second Thread : " +i);                      
       /*taking a delay of one second before displaying next number
       * "Thread.sleep(1000);" - when this statement is executed,
       * this thread will sleep for 1000 milliseconds (1 second) 
       * before executing the next statement.             
       *              
       * Since we are making this thread to sleep for one second,
       * we need to handle "InterruptedException". Our thread
       * may throw this exception if it is interrupted while it 
       * is sleeping.             
       */          
       try         
       {                
          Thread.sleep (1000);          
       }            
       catch (InterruptedException interruptedException)            
       {                
          /*Interrupted exception will be thrown when a sleeping or waiting
           *thread is interrupted.               
           */              
           System.out.println( "Second Thread is interrupted when it is sleeping" +interruptedException);           
       }        
    }   
  }
}

Please check Reference # Threading using Extendfor complete detail, For quick reading i m posting code from above mentioned reference

请检查使用扩展的参考文献 #线程以获取完整详细信息,快速阅读上述参考中的即时发布代码