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

Thread类和Runnable接口实现多线程--2019-4-18

时间:2019-04-25 15:57:43      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:thread   线程   i++   div   static   ble   stat   实现   通过   

1.通过Thread实现

public class TestThread extends Thread{ 
    public TestThread(String name) {
        super(name);
    } 

    public void run() {
        for(int i = 0;i<5;i++){
            for(long k= 0; k <100000000;k++);
            System.out.println(this.getName()+" :"+i);
        } 
    } 

    public static void main(String[] args) {
        Thread t1 = new TestThread("阿三");
        Thread t2 = new TestThread("李四");
        t1.start(); 
        t2.start(); 
    } 
}

2.Runnable接口实现

public class DoSomething implements Runnable {
    private String name;

    public DoSomething(String name) {
        this.name = name;
    } 

    public void run() {
        for (int i = 0; i < 15; i++) {
            for (long k = 0; k < 100000000; k++) ;
            System.out.println(name + ": " + i);
        } 
    } 
}
public class TestRunnable {
    public static void main(String[] args) {
        DoSomething ds1 = new DoSomething("阿三");
        DoSomething ds2 = new DoSomething("李四");

        Thread t1 = new Thread(ds1);
        Thread t2 = new Thread(ds2);

        t1.start(); 
        t2.start(); 
    } 
}

 

Thread类和Runnable接口实现多线程--2019-4-18

标签:thread   线程   i++   div   static   ble   stat   实现   通过   

原文地址:https://www.cnblogs.com/fzly-88/p/10730663.html

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