zoukankan      html  css  js  c++  java
  • 07-多线程

    Java使用java.lang.Thread类代表线程,所有的线程对象都必须是Thread类或其子类的实例。每个线程的作用是完成一定的任务,实际上就是执行一段程序流即一段顺序执行的代码。Java使用线程执行体来代表这段程序流。Java中通过继承Thread类来创建启动多线程的步骤如下:

    1. 定义Thread类的子类,并重写该类的run()方法,该run()方法的方法体就代表了线程需要完成的任务,因此把run()方法称为线程执行体。
    2. 创建Thread子类的实例,即创建了线程对象
    3. 调用线程对象的start()方法来启动该线程

    代码如下:

    测试类:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class  {
    public static void main(String[] args) {

    MyThread mt = new MyThread("新的线程!");
    //开启新线程
    mt.start();
    //在主方法中执行for循环
    for (int i = 0; i < 10; i++) {
    System.out.println("main线程!"+i);
    }
    }
    }

    1.1 继承Thread:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class MyThread extends Thread {
    //定义指定线程名称的构造方法
    public MyThread(String name) {
    //调用父类的String参数的构造方法,指定线程的名称
    super(name);
    }
    /**
    * 重写run方法,完成该线程执行的逻辑
    */

    public void run() {
    for (int i = 0; i < 10; i++) {
    System.out.println(getName()+":正在执行!"+i);
    }
    }
    }

    2.2 实现Runnable接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
     public class MyRunnable implements Ru 大专栏  07-多线程nnable{
     
       public void run() {
         for (int i = 0; i < 20; i++) {
        System.out.println(Thread.currentThread().getName()+" "+i);     
    }      
    }   
    }
    public class Demo {
        public static void main(String[] args) {
            //创建自定义类对象  线程任务对象
            MyRunnable mr = new MyRunnable();
            //创建线程对象
            Thread t = new Thread(mr, "小强");
            t.start();
            for (int i = 0; i < 20; i++) {
                System.out.println("旺财 " + i);
            }
        }
    }

    1.3 匿名内部类方式实现多线程

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     public class NoNameInnerClassThread {
       public static void main(String[] args) {           
    // new Runnable(){      
    // public void run(){          
    // for (int i = 0; i < 20; i++) {              
    // System.out.println("张宇:"+i);                  
    // }              
    // }            
    //    }; //‐‐‐这个整体  相当于new MyRunnable()   
            Runnable r = new Runnable(){
                public void run(){
                    for (int i = 0; i < 20; i++) {
                       System.out.println("张宇:"+i);  
                    }
                } 
            };
            new Thread(r).start();
     for (int i = 0; i < 20; i++) {
               System.out.println("费玉清:"+i);  
            }
       }
    }

    二、线程通信

  • 相关阅读:
    Xn数列(codevs 1281)
    素数密度(洛谷 1835)
    Sightseeing(poj 3463)
    线段树——Ultra-QuickSort
    最大子矩阵
    完成作业的先后顺序
    堆积木块的最大高度
    最长上升序列和
    最长上升子序列
    高精度乘法程序
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12046746.html
Copyright © 2011-2022 走看看