zoukankan      html  css  js  c++  java
  • JAVA 几种多线程的简单实例 Thread Runnable

    实例1:
    class Hello extends Thread{
    private String name;
    public Hello(){}
    public Hello(String name){
    this.name = name;
    }
    public void run(){
    for(int i=0;i<100;i++){
    System.out.println(this.name + i);
    }
    }
    public static void main(String[] args){
    Hello h1 = new Hello("A");
    Hello h2 = new Hello("B");
    h1.run();
    h2.run();
    }
    }
    
    这样的办法输出的结果会是顺序运行,不符合我们想要的多线程运行效果,接下来看 实例2:
    class Hello extends Thread{
    private String name;
    public Hello(){}
    public Hello(String name){
    this.name = name;
    }
    public void run(){
    for(int i=0;i<100;i++){
    System.out.println(this.name + i);
    }
    }
    public static void main(String[] args){
    Hello h1 = new Hello("A");
    Hello h2 = new Hello("B");
    h1.start();
    h2.start();
    }
    }
    
    实例3:
    class Hello implements Runnable{
    private String name;
    public Hello(){}
    public Hello(String name){
    this.name = name;
    }
    public void run(){
    for(int i=0;i<100;i++){
    System.out.println(this.name + i);
    }
    }
    public static void main(String[] args){
    Hello h1 = new Hello("A");
    Thread t1 = new Thread(h1);
    Hello h2 = new Hello("B");
    Thread t2 = new Thread(h2);
    t1.start();
    t2.start();
    }
    }
  • 相关阅读:
    统计代码行数
    梯度下降算法
    multiplot 安装与配置
    ros 源码安装
    cmake 指定gcc/g++版本
    python 科学计算基础库安装
    协方差矩阵的含义
    pysvn 相关
    void 0与undefined
    BEM规范
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7207746.html
Copyright © 2011-2022 走看看