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();
    }
    }
  • 相关阅读:
    ubuntu 11.04 Gnome 恢复默认的任务栏面板
    (转载)学习腾讯的产品管理之道
    (转载)项目管理之外谈项目管理
    windows 下键盘映射
    该留意的文章
    一些常用的工具
    ubuntu 11.04 old sources.list
    一个css3流程导图
    echarts雷达图
    highcharts图表
  • 原文地址:https://www.cnblogs.com/liguangsunls/p/7207746.html
Copyright © 2011-2022 走看看