码迷,mamicode.com
首页 > 编程语言 > 详细

java 两种多线程实现

时间:2017-10-09 11:01:47      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:ica   实现   多线程   log   nbsp   super   ges   es2017   .text   

第一种 继承Thread类

技术分享
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by meicai on 2017/3/17.
 */
public class Hello {
    public static void main(String [] args) {
        System.out.println("begin|线程名:"+Thread.currentThread().getName());
        Person p1=new Person("曹慧明");
        p1.start();
        Person p2=new Person("wt");
        p2.start();
        System.out.println("end|线程名:"+Thread.currentThread().getName());
    }
}

/**
 * 多线程
 * 1.继承Tread类
 * 2.重写run方法
 * 3.调用start方法
 */

class  Person extends Thread
{
    private String name;

    public Person (String name){
        super(name);//给线程起名字
        this.name=name;
    }
    public void run(){
        show(this.name);
    }
   public void show(String name){
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       for(int i=0;i<10;i++) {
           System.out.println(name+i + sdf.format(new Date())+"|线程名:"+getName());
       }
   }
}
第一种 继承Thread类

 技术分享

java 两种多线程实现

标签:ica   实现   多线程   log   nbsp   super   ges   es2017   .text   

原文地址:http://www.cnblogs.com/caohuimingfa/p/7639865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!