java多线程简单小例子2——实现Runnable接口

/** 
* 实现Runnable接口的类
* 
* @author 
*/ 
public class DoSomething implements Runnable {
    private String name;

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

    public void run() {
        for (int i = 0; i < 5; i++) {
            for (long k = 0; k < 100000000; k++) ;
            System.out.println(name + ": " + i);
        } 
    } 
}
/** 
* 测试Runnable类实现的多线程程序
* 
* @author 
*/ 
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(); 
    } 
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。