zoukankan      html  css  js  c++  java
  • 通过实现Runnable接口来实现多线程

     1 package testBlog;
     2 
     3 class MyThread implements Runnable{//这种线程创建方式可以避免单继承局限
     4     String name;
     5     
     6     MyThread(String name){
     7         this.name = name;
     8     }//用构造器给线程起名
     9     
    10     public void run() {//这里要记得覆写run()方法
    11         for(int x = 0;x<20;x++) {
    12             System.out.println(this.name+"--->"+x);
    13         }
    14     }
    15     
    16 }
    17 public class Test{
    18     public static void main(String[] args) {
    19         MyThread mt1 = new MyThread("线程A");
    20         MyThread mt2 = new MyThread("线程B");
    21         MyThread mt3 = new MyThread("线程C");
    22         
    23         //Runnable接口只能用run()方法来启动,但可以通过Thread类匿名对象的形式,来使用start()方法
    24         mt1.run();
    25         (new Thread(mt2)).start();
    26         (new Thread(mt3)).start();
    27         
    28     }
    29 }
  • 相关阅读:
    information_schema
    面包屑路径导航
    mysql5.7.26安装
    菜单权限作为父权限
    权限控制到按钮
    二级菜单
    留言板和jq轮播图
    M商城
    表单
    w3c
  • 原文地址:https://www.cnblogs.com/ssC2H4/p/8186334.html
Copyright © 2011-2022 走看看