zoukankan      html  css  js  c++  java
  • 【多线程】-实现多线程的三种方法

    1.第一种是实现 Runnable,并重写run方法

      Thread类中构造方法

    /**
    * Allocates a new {@code Thread} object. This constructor has the same
    * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
    * {@code (null, target, name)}.
    *
    * @param target
    * the object whose {@code run} method is invoked when this thread
    * is started. If {@code null}, this thread's run method is invoked.
    *
    * @param name
    * the name of the new thread
    */
    public Thread(Runnable target, String name) {
    init(null, target, name, 0);
    }

      

     1 package com.idcos.automate.thread;
     2 
     3 import com.idcos.Application;
     4 import org.junit.runner.RunWith;
     5 import org.springframework.boot.test.SpringApplicationConfiguration;
     6 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     7 import org.springframework.test.context.web.WebAppConfiguration;
     8 
     9 /**
    10  * @author GuanBin
    11  * @version ThreadDemo1.java, v1 2018/5/2 下午5:43 GuanBin Exp $$
    12  */
    13 @RunWith(SpringJUnit4ClassRunner.class)
    14 @SpringApplicationConfiguration(classes = Application.class)
    15 @WebAppConfiguration
    16 public class ThreadDemo1 implements Runnable {
    17 
    18     private Thread t;
    19 
    20     private String threadName;
    21 
    22 //    public ThreadDemo1() {
    23 //
    24 //    }
    25 
    26     public ThreadDemo1(String name) {
    27         threadName = name;
    28         System.out.println("Creating threadName" + threadName);
    29     }
    30 
    31     @Override
    32     public void run() {
    33 
    34         System.out.println("Running threadName" + threadName);
    35         try {
    36             for (int i = 4; i > 0; i--) {
    37                 System.out.println("this thread" + i);
    38                 Thread.sleep(100);
    39             }
    40 
    41         } catch (InterruptedException e) {
    42             System.out.println("This thread is Interrupted");
    43         } catch (Exception e) {
    44             System.out.println(e);
    45         }
    46 
    47         System.out.println("Thread is exiting");
    48 
    49 
    50     }
    51 
    52     public void start() {
    53         System.out.println("Starting method name" + threadName);
    54         if (t == null) {
    55             t = new Thread(this, threadName);//初始化一个Thread
    56             t.start();//线程开始
    57         }
    58     }
    59 
    60 //    @Test
    61     public  static void main(String[] args) {
    62         ThreadDemo1 threadDemo1 = new ThreadDemo1("testThread1");
    63         threadDemo1.start();//调用ThreadDemo1中的start方法
    64 
    65         ThreadDemo1 threadDemo2 = new ThreadDemo1("testThread2");
    66         threadDemo2.start();
    67     }
    68 }

    输出为:

    2.第二种方法是继承Thread类,并重写其run方法,也是必须要调用start方法

    其实,本质上也是间接实现了实现了Runnable接口,因为Thread也是实现了Runnable接口

     1 package com.idcos.automate.thread;
     2 
     3 /**
     4  * @author GuanBin
     5  * @version ThreadDemo2.java, v1 2018/5/3 上午11:14 GuanBin Exp $$
     6  */
     7 public class ThreadDemo2 extends Thread {
     8 
     9     private Thread t;
    10 
    11     private String threadName;
    12 
    13 //    public ThreadDemo1() {
    14 //
    15 //    }
    16 
    17     public ThreadDemo2(String name) {
    18         threadName = name;
    19         System.out.println("Creating threadName" + threadName);
    20     }
    21 
    22     @Override
    23     public void run() {
    24 
    25         System.out.println("Running threadName" + threadName);
    26         try {
    27             for (int i = 4; i > 0; i--) {
    28                 System.out.println("this is " + i);
    29                 Thread.sleep(100);
    30             }
    31 
    32         } catch (InterruptedException e) {
    33             System.out.println("This thread is Interrupted");
    34         } catch (Exception e) {
    35             System.out.println(e);
    36         }
    37 
    38         System.out.println("Thread is exiting");
    39 
    40 
    41     }
    42 
    43     public void start() {
    44         System.out.println("Starting method name" + threadName);
    45         if (t == null) {
    46             t = new Thread(this, threadName);
    47             t.start();
    48         }
    49     }
    50 
    51     //    @Test
    52     public  static void main(String[] args) {
    53         ThreadDemo2 thread1 = new ThreadDemo2("thread1");
    54         thread1.start();
    55 
    56         ThreadDemo2 thread2 = new ThreadDemo2("thread2");
    57         thread2.start();
    58     }
    59 }
    第二种实现方法

     

    作者:guanbin —— 纵码万里千山

    出处:https://www.cnblogs.com/guanbin-529/

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    String类的常用方法(P小写)
    二维数组:判断是否有目标数
    java实现输入年份判断在哪一天(正则表达式待改进)
    Java实现八进制正整数转化为十进制数
    时钟和定时器
    电路的频率响应---带宽的定义
    stm32两轮平衡车资料
    二阶常系数齐次线性微分方程的解法
    同步积分
    陀螺仪信号解调
  • 原文地址:https://www.cnblogs.com/guanbin-529/p/8984313.html
Copyright © 2011-2022 走看看