zoukankan      html  css  js  c++  java
  • Java中 实现多线程成的三种方式(继承,实现,匿名内部类)

    ------------------------------------------------------------------------------------------------------------    

       

     1  /**  第一种方式:继承Thread类
     2 
     3     *             1. 定义一个类,然后让该类继承Thread类
     4 
     5     *             2. 重写run方法
     6 
     7     *             3. 创建定义的这个类的对象
     8 
     9     *             4. 启动线程
    10 
    11     */
    12 
    13  //继承Thread类
    14 
    15   public class MyThread extends Thread{
    16     
    17     public MyThread() {}
    18 
    19     public MyThread(String name) {
    20         super(name);
    21     }
    22 
    23     //run方法中封装的应该是要被线程执行的代码 ,run方法中代码的原则,一般存方的都是比较耗时的代码
    24     public void run (){
    25         
    26         for (int i = 0; i < 200; i++) {
    27             
    28             System.out.println(Thread.currentThread().getName() + "---" + i);
    29         }
    30     } 
    31 }
    32 
    33   //测试方法
    34 
    35   private static void Test() {
    36 
    37    // 创建对象
    38         MyThread mt1 = new MyThread();
    39         MyThread mt2 = new MyThread();
    40 
    41 
    42         //启动线程
    43         // public void start()使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
    44         mt1.start();
    45         //mt1.start();// 线程的启动只能是一次
    46         mt2.start();
    47 
    48   }

    ------------------------------------------------------------------------------------------------------------

     1  /**  第二种方式:实现Runnable接口
     2      避免了单线程带来的困扰
     3      *    (1): 创建一个类,让该类实现Runnable接口
     4 
     5      *    (2): 重写run方法
     6 
     7      *    (3): 创建该类的对象
     8 
     9      *    (4): 创建Thread类的对象,然后把3中的对象作为参数传递给Thread
    10 
    11      *    (5): 启动线程
    12 
    13      */
    14 
    15   //实现Runnable接口
    16 
    17   public class MyThread2 implements Runnable {
    18    
    19     public MyThread2() {
    20         super();
    21       }
    22 
    23       public void run() {
    24         
    25           for(int x = 0 ; x < 200 ; x++){
    26             
    27              System.out.println(Thread.currentThread().getName() + "---" + x);
    28         }
    29     }
    30 }
    31 
    32   //测试方法
    33 
    34     private static void Test2() {
    35 
    36      // 创建MyThread的对象
    37         MyThread2 mt = new MyThread2();
    38         // 创建Thread类的对象,然后把3中的对象作为参数传递给Thread
    39         // public Thread(Runnable target)
    40         Thread t1 = new Thread(mt,"张飞");
    41         Thread t2 = new Thread(mt,"关羽");
    42         Thread t3 = new Thread(mt,"刘备");
    43         // 启动线程
    44         t1.start() ;
    45         t2.start() ;
    46         t3.start() ;
    47     }

    ------------------------------------------------------------------------------------------------------------

      

     1  /**  第三种方式:使用匿名内部类的方式实现
     2     很少见
     3      *    new 类名/接口名() {      
     4 
     5      *      方法重写 ;
     6 
     7      *    } ;
     8 
     9      */
    10 
    11  //测试方法
    12 
    13  //匿名内部类
    14 
    15     @Test
    16     private static void Test3() {
    17 
    18     new Thread() {
    19            public void run() {
    20                 System.out.println("线程执行了。。。。。。。");
    21             }
    22         }.start();
    23     }

    ------------------------------------------------------------------------------------------------------------

  • 相关阅读:
    the core or essence of a computer
    HEXADECIMAL NOTATION is Syntactic_sugar.
    Converting from Decimal Notation to Binary Notation for Fractions
    convert from base 10 to base 2
    MYSQL PASSWORD()
    Environment Variables
    Why Stored Procedures?
    delimiter
    page fault rate
    Segmentation
  • 原文地址:https://www.cnblogs.com/rongsnow/p/5152675.html
Copyright © 2011-2022 走看看